Extra: Basics of JavaScript¶
Note
This lab will be covered on Thursday 4/13
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!
Let’s a-(variable)-go!¶
Variables are like boxes that hold information. They can be numbers, text, or even collections of other variables! In programming languages we call those variable types. With JavaScript, variables are automatically assigned types based on their declaration. We’ll discuss more next week, but what you need to know for now is how to declare variables.
In JavaScript all declarations and lines should end with a semicolon ;
, which is like a .
in English that says, my statement is done.
This is an example 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!
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 #3 - 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
.
Hello Leaflet… Finally..¶
OK, why did we do ALL of that? Well, when we use Leaflet, we actually need to bring in Leaflet’s external CSS and JavaScript files!
So, in our header, let’s add the following:
<!-- Leaflet's css-->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
<!-- Leaflet's JavaScript-->
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
Now, let’s go ahead and add a container for our map.
After <div id="main"></div>
add a new <div></div>
tag, and give it an ID attribute of “map”:
With our container ready to go, open up the JavaScript file again and add the following Leaflet code template:
L.
is theLeaflet
class that allows us to use built-in Leaflet tools. The.
is like a chain of commands, but similar tocss
, Leaflet wants us to knowwhere our map is!
In the documentation, the map constructor expects an ID, which we calledthe_map
. We then usesetView
to set theLatitude (y)
,Longitude (x)
, andZoom
of the initial map.- We use a
L.tilelayer
class to add a basemap to our map. - We use the
L.marker
to add a point to our map. Notice there is no;
here because the code continues.
Can you see the usage of latitude and longitude in the code?
What is latitude and Longitude
What is L.map
and L.tile
?¶
L.map
is Leaflet’s lingo for its own mapping Application Programming Interface (API). Every API has its own unique language to utilize it. To learn more about Leaflet’s API visit here:
https://leafletjs.com/reference-1.7.1.html
⚽ Class Exercise #4 - Adding more markers¶
Tasks
- Add some new markers!
- Customize the initial map
- Add a new element
<div id="contents">
inside themain
element. - Optional: change the base map or add some
html
into the marker popups.
Looking at the code above a little bit, we can see some latitude/longitude pairs. Your task is to copy the marker code add more markers of your choosing.
Unique variable names
When you create new marker variables, you must give the marker variable a new name, like marker2
or you will simply override the previous marker!
To find latitude/longitude of coordinates, you can use this website or another tool or your choosing:
Optional: Not happy with the basemap?¶
See if you can switch the basemap out by visiting the following link and changing L.tileLayer
on your map:
🏁Checkpoint¶
Check to see if your code looks likes the following before moving on: