Basics of JavaScript¶
JavaScript makes sure our page knows how to function and react. There are different frameworks for JavaScript, like React.js and vue.js, but this class will be focusing on vanilla JavaScript with ES6+ standards.Read more about the standards here.
In HTML
, JavaScript
must be contained within a script tag. In our <head>
tag, let’s add a <script></script>
tag.
Any Javascript that is in the <head>
tag will load first.
Any JavaScript that is in the <body>
tag will load later.
JavaScript functions to run after the HTML body
loads, so putting the <script>
after the </body>
becomes necessary.
This will be relevant when we bring in Leaflet.js
because the Leaflet
library needs to be loaded first! That means it should go in the header, while our own custom JavaScript
comes after, preferrably later in the <body>
tag, you can even kick our JavaScript file out of the body and put it into a <footer>
tag!
It’s a me Lego Var
-io¶
Variables are like boxes that hold information, try to think of them as the building blocks of the instructions.
Variables are the Lego blocks of programming and can be numbers, text, or even collections of other variables! In programming languages we call variables can be classified into types. In order to use a variable, you have to declare variables. With JavaScript, variables are automatically assigned types based on their declaration.
In JavaScript all declarations and lines should end with a semicolon ;
, which is like a . (period)
in English that says, my statement is done.
These are examples of a declaration:
In front you see the var
keyword that tells the web browser, “Hey this is a variable!”. In this example, day
is a numeric type with a value of 8
and name
is a string type. Each type has certain properties and uses, for example you can add numbers together using something like day + day
, but you adding strings will simply concatenate and not total them.
What is a keyword?
In most coding languages, a keyword is a word that tells a program to treat the following text, numbers, or characters in a specific way. For example, var myName
says treat myName
as a variable. This means you CANNOT name a variable var
, Jar Jar Binks cousin Var Var Binks is VARy bad for JavaScript to see! i.e. var var
Also note, you cannot use spaces
in variable names!
LET’s a go!¶
With JavaScript ES6, let
and const
keywords were introduced to declare variables. This change means that the recommend practice is to no longer use the var
keyword. let
and const
variables get declared in the same way:
- The
let
keyword LETS a variable CHANGE! - The
const
keyword declaration keeps a variable CONSTant!
Let vs Const vs Var¶
What is the difference?
- The
let
keyword declaration LETS a variable change - The
const
keyword delcaration a variable CONSTant and will never change. - The
var
allows varaibles to change or never change depending on where it was declared! VERY PROBLEMATIC!
Because var
can be changing (mutable) and unchanging at the same time, so var
was changed into off into two different variable types, let
and const
.
Scopes: Local vs. Global
Where you declare
a variable sets the scope to either a local one (limited to a function or area in the code) or global (can be accessed by anything/anywhere else in the code).
So, bye bye var
and LET
us welcome our new CONST
variables to the JavaScript programming world.
TLDR
DO NOT USE var
unless you need to code for Internet Explorer.
Console.log()¶
By itself, our script tag does nothing. So, one VERY helpful JavaScript tool (method) that we should familarize ourself with is console.log()
, because it allows us to test our code.
Add the following script:
Nothing happened?! What!?¶
Actually, you are about to unlock your full web developer potential!
In Firefox, right click anywhere on the page and the click Inspect Element
:
This opens the Developer Toolbar!! 🎉🎉 You can also find it by going to the Menu and going to Web Developer and then Web Developer Tools.
Click on the Console button:
Yay! Our message is there!
Linking to another JavaScript file¶
Similar to the CSS files, we should move the JavaScript file into its own file (and folder) to avoid cluttering the HTML file with JavaScript.
Importing different libraries, whether it it CSS
or JavaScript
is the main way unlock skills and level up our webpage.
BUT!!! Instead of the <link>
that we use with CSS
we use the <script>
tag:
⚽ In-class Exercise #1 - JuSt link your JS file¶
Tasks
- Create a new folder called
js
- Add a
JavaScript
file in there calledinit.js
- Add JavaScript method:
console.log()
with a message of your choosing - Get your message to show up in the console
Answer
- Click on the
New Folder
button:
- Type in
js
:
- Click on the New File button:
- Give it a name, like
init.js
, which in this case stands for theinitial JavaScript file of our page
- In the
index.html
file and before the end of the<body>
element include the following:
Important!
Never include <script></script>
tags inside of a Javascript file, those are HTML tags
!!! Do so will break your page, because you are mixing two different languages: HTML
with JavaScript
.
🏁Checkpoint¶
Check to see if your code looks likes the following before moving on:
js/init.js | |
---|---|