An introduction to the basics.
Front End Engineer
HubSpot
alecortega
slides.com/alecortega/javscript-foundations/live
What is JavaScript?
Variables and operators
Conditional Logic
Looping
If you don't understand something it's my fault, not yours.
If you need me to break something down or explain in different terms just let me know!
Creates the structure of your page.
Decorates that structure with styling and gives it the appearance you want.
Makes everything interactive, gives you the ability to change your page after it has already loaded.
It allows us to change the page based on user interaction and turn static web pages into rich and dynamic applications.
2 * 2;2 * 2;How does the computer read this?
It starts with the number two
It then sees a symbol, in JavaScript we call these special symbols operators. I.e. +, -, =, !, *, /
It then takes the operator and uses the value to right of it and applies it to the value on the left of it.
It wraps things up with a semicolon. You can think of these as you would periods in English.
This entire line is called a statement. And all JavaScript is, is a series of statements.
We're going to cover three of them today.
2;
3.14159;
0.005;We can create an number just by writing out the number we want. Numbers can include decimals as well.
"hello world";
'I have 0 dogs, but I want 50.';
"123";String is what we call words in JavaScript. We can declare strings by wrapping our words in single or double quotes.
true;
false;Boolean can only have two values, true or false. This will come into play once we start talking about data flow.
2 * 2;main.js
2 * 2;
2 * 3;
2 * 4;If we ran this code, we wouldn't actually see it do anything. It is performing the calculations but we need to do something special in order to see what's happening here.
2 * 2;
2 * 3;
console.log(2 * 4);console.log(2 * 4);I'll explain the syntax here a little later. But for right now just know that whatever you put between the parenthesis will show up in your console when your code is run.
main.js
Log the word hello to the console.
Do the above and log the summation of 1 and 5.
Do the above and log the summation of 1 and the word hello.
Do the above and log the summation of 1 and true.
1 + true;
=> 2;Unlike other languages, JavaScript doesn't yell at you when you try to do something like this. Instead it tries to be smart and tries to convert one value into another in order for the result to make some sort of sense.
console.log('hello');
console.log(1 + 5);
console.log('hello' + 5);
console.log(1 + true);
console.log('hello')
console.log(1+5)
console.log('hello' + 5)
console.log(1+ true)
Questions?
Bob
Ferrari
Bob
Honda
Ferrari
Bob
Ferrari
Variable
Value
Variables refer to a value, but that reference can change at any time. The variable itself doesn't change, only it's reference.
var favoriteCar;The name given to the variable follows the keyword and it should be descriptive of the value that it references.
var favoriteCar;
favoriteCar = 'Ferrari';var favoriteCar;
favoriteCar = 'Ferrari';
favoriteCar = 'Honda';var favoriteCar;
favoriteCar = 'Ferrari';
favoriteCar = 'Honda';1) We create a variable named favoriteCar.
2) We assign a reference to the String 'Ferrari'.
3) We re-assign a reference to the String 'Honda'.
var favoriteCar = 'Ferrari';var favoriteCar = 'Ferrari';
console.log(favoriteCar);var favoriteCar = 'Ferrari';
console.log(favoriteCar);
=> 'Ferrari'Bob
Variable
Value
var favoriteCar;
console.log(favoriteCar);var favoriteCar;
console.log(favoriteCar);
=> undefinedvar colorOfSky = 'blue';
var currentFloor = 20;
var someCoolOperation = 5 * 10 / 2 - 15;
var areCrocsTrendy = false;
When you use a variable, you can think of it as a placeholder for the value it references.
var currentFloor = 20;
console.log(1 + currentFloor);var currentFloor = 20;
console.log(1 + 'currentFloor');var currentFloor = 20;
console.log(1 + 'currentFloor');
=> '1currentFloor';1 === 1
1 !== 2
3 > 1
4 <= 10Equal to
Not equal to
Greater than
Less than or equal to
Using a comparison operator will result in either true or false.
1 !== 2 && 1 === 1
3 > 5 || 1 < 2AND
OR
This will also result in a single boolean value.
var areCrocsTrendy = false;
console.log(areCrocsTrendy === true);Log "dataOne" to the console.
Log "dataTwo" to the console.
Log if "dataOne" and "dataTwo" are equal to each other.
Create a variable named "dataThree". Set it equal to "dataOne". Log if "dataOne" is equal to "dataThree".
dataOne
dataTwo
{}
{}
dataThree
Questions?
Imagine if we have a program that will log "good night" or "good morning!" based on the time of day. How do we make it do one or the other?
Is it night?
console.log('good night!');
console.log('good morning!');
We can't stop the path but we can "control" where the path "flows" based on conditional statements.
// If it is night
// log "good night!"
// else
// log "good morning!"// If it is night
// log "good night!"
// else// If it is night
// log "good night!"// If it is nightvar isNight = true;
// If it is night
if (isNight) {
// log "good night!"
console.log('good night!');
}
// else
else {
}
var isNight = true;
// If it is night
if (isNight) {
// log "good night!"
console.log('good night!');
}
// else
else {
// log "good morning!"
console.log('good morning!');
}
var isNight = true;
// If it is night
if (isNight) {
// log "good night!"
console.log('good night!');
}
var isNight = true;
// If it is night
if (isNight) {
}
var isNight = true;
if (isNight) {
console.log('good night!');
}
else {
console.log('good morning!');
}
Starts the conditional statement with "if".
It will check whatever is between the parenthesis for something that evaluates to a true or false value.
The brackets tell the interpreter "this is the block of code that I want you to use for the previous statement". Imagine "{" means start and "}" means stop.
var isNight = false;
var isMorning = true;
if (isNight) {
console.log('good night!');
}
else if (isMorning) {
console.log('good morning!');
}
else {
console.log('good day!');
}
You can use "else if" to keep conditionally values until one of them is true. If none of them are true it will always default to an "else" statement if it exists.
var isNight = false;
var isMorning = true;
if (isNight) {
console.log('good night!');
}
else if (isMorning) {
console.log('good morning!');
}
else {
console.log('good day!');
}
var isNight = false;
var isMorning = false;
if (isNight) {
console.log('good night!');
}
else if (isMorning) {
console.log('good morning!');
}
Questions?
console.log(1);console.log(1);
console.log(2);
console.log(3);
console.log(4);
console.log(5);
...
var currentNumber = 0;
while (currentNumber < 5) {
console.log(currentNumber);
}var currentNumber = 0;var currentNumber = 0;
while (currentNumber < 5) {
}var currentNumber = 0;
while (currentNumber < 5) {
console.log(currentNumber);
}var currentNumber = 0;
while (currentNumber < 5) {
console.log(currentNumber);
currentNumber = currentNumber + 1;
}=> 0
=> 0
=> 0
=> 0
...
=> 0
=> 1
=> 2
=> 3
=> 4
for (var i; i < 10; i++) {
console.log(i);
}Crate a loop that logs every number from 0 to 100
Modify that loop so that it logs every even number from 0 to 100.
Modify that loop so that logs "Almost there!" after it logs "90".
Questions?
What are truthy and falsey values? Can you give examples of some?
What is "null" and how is it different than undefined?
What is JavaScript?
Variables and operators
Conditional Logic
Looping
slides.com/alecortega/javascript-foundations