01_SettingUp.js 02_TheConsole.js
console.log()method tells the web console to output a message- The message we want to display should be inside parentheses
( )and surrounded by quotes," "or' ' - To run a file, type
node .\filename.jsin Terminal
Example:
console.log("π Howdy!");
console.log('π Howdy!');Note: It's optional to include a ; semicolon at the end of a line in JavaScript.
- Output multiple messages by using multiple
console.log()methods
console.log("π Greyhound to New York");
console.log("π₯ Departs at 9:30 am");- Uses
//
// console.log("Nada");
console.log("Hi"); // I'm a comment, too!- Create a multi-line comment using
/*to start the comment and*/to end it
/* This is commented out.
console.log("Nada.")
Yep! This is, too.
console.log("Not displayed, either.")
*/- The
console.log()method outputs messages to the Console window. - JavaScript code runs one line at a time, from top to bottom.
- Single line comments are created using the
//forward slashes. - Multi-line comments are started by
/*and closed with*/.
06_LetAndConst.js 06_Example1.js
- Variables are used for storing data values that can be used later in the program
- Each variable has a name and they hold a value
letis used for variables where the value will changeconstis used for variables where the value will stay constant
keyword name = value;
const userId = "1618033988";
let userName = "Leeroy Jenkins";- Variables with more than one word are written in camelCase (ex. userName)
=equal sign means assignment
Example: let allows a change in value, or to print it out
let userName = "Leeroy Jenkins";
userName = "Leeroy05";
console.log(userName); // Output: Leeroy05Example: const we cannot change it's value, but we can still print it out
const userId = "16180339887";
console.log(userId); // Output: 16180339887Example: An error for using const and changing the value
// SyntaxError: Missing initializer in const declaration let year = 2023;
let age = 28;
let mealCost = 12.99;
const pi = 3.14;
const daysOfWeek = 7;- Uses
" "or' '
let message = "good nite";
let user = '@zuck';
const company = "CodΓ©dex";- Uses
bool, stores a value that can be only eithertrueorfalse
let hungry = true;
let lateToMeeting = false;
const earthIsFlat = false;- Any variable that is declared but hasn't yet received a value is
undefined
let startDate = "01/23/2022";
let endDate;
console.log(startDate); // 01/23/2022
console.log(endDate); // undefined+Addition-Subtraction*Multiplication/Division%Modulo (returns the remainder)**Exponent (covered in the next exercise)
Example 1
let score = 0; // Declare score and initialize it
score = 4 + 3; // score is now 7
score = 4 - 3; // score is now 1
score = 4 * 3; // score is now 12
score = 4 / 3; // score is now 1.3333
score = 4 % 3; // score is now 1
console.log(score); // Output: 1Example 2
const pizza = 2.99;
const coke = 0.99;
let total = pizza + coke;
let tip = total * 0.2;
console.log(tip); // Output: 0.796
OR
let tip = (pizza + coke) * 0.2;- Use the
**notation for exponententiation
let score = 0;
score = 2 ** 2; // score is 4
score = 2 ** 3; // score is now 8
score = 2 ** 4; // score is now 16
score = 2 ** 5; // score is now 32
console.log(score); // Output: 32Note: In the order of operations, exponents are calculated before multiplication/division and addition/subtraction.
letandconstkeywords- Data types: Numbers, Strings, Boolean, Null, and Undefined
- Arithmetic operators:
+,-,*,/ %modulo operator finds the remainder of division between two numbers**exponent operator raises one number to the power of another number
ifkeyword, which tests a condition for truth and executes the code if it istrue
if (condition) {
// Do something
}If the condition is true, then the statements within are executed. If the condition is false, then nothing happens and the program continues on after the if statement
Example
if (grade > 60) {
console.log("You passed!");
}- An
elseclause can be optionally added to the end of anifstatement
if (condition) {
// Do something
} else {
// Do something else
}- If the condition is
true, execute the code inside theif - Else the condition is
false, execute the code inside theelse
Example
if (grade > 60) {
console.log("You passed.");
} else {
console.log("You failed.");
}===strict equal!==strict not equal>greater than>=greater than or equal<less than=less than or equal
- You can add an
else ifsection between the if and else in your control flow - This gives your program more conditions to evaluate, besides just two
if (conditionA) {
// Do this
} else if (conditionB) {
// Do this, instead
} else {
// Do this if none of the above are true
}Example
let grade = 93;
if (grade > 90) {
console.log("A");
} else if (grade > 80) {
console.log("B");
} else if (grade > 70) {
console.log("C");
} else if (grade > 60) {
console.log("D");
} else {
console.log("F");
}- In JavaScript, the
Math.random()method returns a random decimal number between 0 and 1
Usage:
console.log(Math.random());
console.log(Math.random());
console.log(Math.random());Output is something like:
0.0236966238022709
0.7397680104909345
0.4103022944399179Example
Math.floor(Math.random() * 10);- Logical operators, also known as Boolean operators, combine and evaluate two conditions. They are
&&,||, and!operators - The
ANDlogical operator&&returns true if both conditions are true, and returns false otherwise. - The
ORlogical operator||returns true if at least one of the conditions is true, and false otherwise. - The
NOTlogical operator!returns true if the condition is false, and vice versa.
Example
if (hunger > 4 && anger > 1) {
console.log("Hangry");
}
if (coffee > 0 || bubble_tea > 0) {
console.log("π");
}
if (!tired) {
console.log("Let's code!");
}Table Summary
| A | B | A && B | A ll B |
|---|---|---|---|
| false | false | false | false |
| false | true | false | true |
| true | false | false | true |
| false | true | true | true |
17_Example.js 17_RockPaperScissor.js
- Conditionals control the order in which the program's code executes
ifstatement checks a condition for truth and executes the code if it iselseexecutes the code if none of the above is True- One or more
else ifs can be added between if and else - Comparison operators compare two values:
===,!==,>,>=,<,<= - Logical operators combine two or more conditions:
&&,||,!
- Loops are used to repeat a block of code based on a certain condition
How loops work is similar to how cars may circle a roundabout and leave when a certain condition is met (like taking the desired exit)
let randomNumber = Math.floor(Math.random() * 10);
while (randomNumber != 7) {
console.log("Duck π¦");
randomNumber = Math.floor(Math.random() * 10);
}
console.log("Goose! π¦’");- The
whileloop uses a condition that is either true or false
while (condition) {
// Code here
}- As long as the condition is
true, the code in the while loop will run. Otherwise, the loop ends when the condition becomesfalse.
- To "iterate" means to run the code in a loop once
- When the
iiterator variable has reached a value that sets the loop's condition tofalse, we can successfully exit the loop
let i = initialValue;
while (condition) {
// Update the i iterator variable
} for (let i = initialValue; condition; updateIterator) {
// Code here
}- We define an
iiterator variable with aninitialValue - Before the first or next iteration, we test the
ivariable in the condition statement. If we get true, we run the code. Otherwise, we exit the loop - After each iteration, we
updateIteratorand change the value ofi
As the value of i changes, it should eventually cause the condition statement to return false so we can exit the for loop.
for (let i = 1; i <= 10; i++) {
console.log(i);
}let i = 1: initializes an iterator variable.i <= 10: the conditional statement that is tested before each iteration.i++: this increments the iterator variable by 1 after each iteration.
Note: The ++ operator increments a number value by 1. It's the same as writing i = i + 1.
Output would be:
1
2
3
4
5
6
7
8
9
10
Because we want i to change each time in the loop, we use the let keyword. We use a logical evaluation to determine if the current value of i is less than or equal to 10. After each iteration, we add 1 to the value of i (i.e., i++).
Example
for (let i = 1; i <= 3; i++) {
console.log("Beetlejuice!")
}
// Output:
// Beetlejuice!
// Beetlejuice!
// Beetlejuice!- If a given condition is
true, thecontinuekeyword ends the current iteration in a givenfororwhileloop and we go to the next one.
for (let i = 0; i < 5; i++) {
if (i == 1) {
continue;
}
console.log(i);
}The code above will output the numbers 0 through 4, except for 1 because the continue skips this iteration.
Output would be
0
2
3
4
- Ends loops early is
break - If a given condition in a loop is
true, this keyword ends the loop altogether and the program resumes on the line immediately after the closing curly bracket of the loop
for (let i = 0; i < 5; i++) {
if (i == 3) {
break;
}
console.log(i);
}If and when i is equal to 3, the break keyword ends the loop, and our output looks like this:
0
1
2
- You can use loops to perform the same operations over and over
- A
whileloop runs as long as a condition istrue - A
forloop runs a certain amount of times, based on an iterator variable - The
continuekeyword ends the current iteration in a loop, given atruecondition - The
breakkeyword exits the loop entirely, given atruecondition
Arrays are variables that can hold multiple values in a comma-separated list:
array = ["Leonardo", "Michaelangelo", "Donatello", "Raphael"];- The elements used for our array can either be a mix of data types, or of a single data type
let lotteryNumbers = [13, 36, 45, 57, 67, 14]; // btw call us if you have these numbers!
const lettersAndNumbers = ["a", "b", "c", 1, 2, 3];- To access a single element in an array, we use bracket notation
let fruits = ["strawberries π", "blueberries π«", "bananas π"];
console.log(fruits[0]);- Array indices start at 0, not 1
To change a value of an existing element:
let pokemon = ["Bulbasaur", "Charmander", "Squirtle"];
pokemon[2] = "Pikachu";
console.log(pokemon);
// Output: ["Bulbasaur", "Charmander", "Pikachu"]To assign a new value to an index that doesn't exist yet
let numbers = [1, 2, 3];
numbers[3] = 4;
console.log(numbers);
// Output: [1, 2, 3, 4]Note: Be careful when creating new elements like this, for you may end up creating undefined elements between the old last element and the new last element
let numbers = [1, 2, 3];
numbers [5] = 88;
console.log(numbers);
// Output: [1, 2, 3, undefined, undefined, 88]- To know how many elements are in the array
.lengthand produces an integer that tells us how long an array is (or how many elements it has)- Used with a dot notion
arrayName.length; const testScores = [78, 96, 100, 88, 85, 81, 79];
const numberOfScores = testScores.length;
console.log(numberOfScores); // Output: 7.lengthis commonly used withforloops
const musicNotes = ["Do", "Re", "Mi", "Fa", "So", "La", "Ti"];
for (let i = 0; i < musicNotes.length; i++) {
console.log(musicNotes[i]);
} Using bracket notation, we can access the next element in the array with the i loop variable.
Note: We named the iterator variable i but we can technically give this any name as long as it is consistent throughout the loop.
const musicNotes = ["Do", "Re", "Mi", "Fa", "So", "La", "Ti"];
let counter = 0;
while (counter < musicNotes.length) {
console.log(musicNotes[counter]);
counter++;
}- Methods are pieces of code that some variables can use to perform specific actions, like produce a new value
Note: The first method we encountered was console.log() from Chapter 1
const bandMembers = ["J.C.", "Joey", "Chris", "Lance", "Justin"];
bandMembers[1] = "Nick";
console.log(bandMembers);
// Output: ["J.C.", "Nick", "Chris", "Lance", "Justin"] myArray.unshift(newValue);
myArray.push(newValue);-
The
newValueis known as a parameter that we pass into the method. Some methods use parameters while others do not. -
You can also add multiple elements with the
.push()and.unshift()methods by separating each new value with a comma (i.e.,.push(newValue1, newValue2);)
Note: For every new element added to an array with the .unshift() method, the indices of the other elements are shifted up by 1
- The
.shift()method removes and returns the first element in an array, shifting the indices of the remaining elements back by 1
Conversely, the .pop() method removes and returns the last element of an array:
const greeks = ["Zeus", "Hera", "Poseidon", "Apollo", "Hermes", "Dionysus", "Hades"];
const shiftedElement = greeks.shift();
const poppedElement = greeks.pop();
console.log(shiftedElement);
console.log(poppedElement);
console.log(greeks);
/*
Output:
Zeus
Hades
["Hera", "Poseidon", "Apollo", "Hermes", "Dionysus"]
*/Note: By "returns", we mean that greeks.shift() produces a single value, the removed first element, that can be used elsewhere in the program (such as being printed or stored in a variable)
- Index
[i]is used to obtain a value, while.indexOf()is used to find the index of a particular value.
const myArray = [1, 2, 3];
console.log(myArray[1]); // Output: 2
vs.
const stories = [
"Sorcerer's Stone",
"Chamber of Secrets",
"Prisoner of Azkaban",
"Goblet of Fire",
"Order of the Phoenix",
"Half-blood Prince",
"Deathly Hallows"
];
console.log(stories.indexOf("Half-blood Prince")); // Output: 5- If the array doesn't have the value we're looking for, -1 is returned instead.
Note: If there are multiple elements with the same value in an array, the .indexOf() returns the index of the first occurrence of that value.
.includes()method checks if an element with the value parameter exists in the array, and returns a booleantrueif it does orfalseotherwise
const myArray = [1, 2, 3];
console.log(myArray.includes(3));
console.log(myArray.includes(42));
/*
Output:
true
false
*/- Creating, accessing, and updating an array with square brackets.
- Looping over arrays with the
.lengthproperty - Adding and removing array elements with various methods:
.push().unshift().shift().pop()
- Confirming if a given element or value exists in an array with the
.includes()and.indexOf()methods.
- Functions are blocks of code that we write once and can use later on in our programs
- Used in separating tasks into smaller chunks that can be executed multiple times via the function name
function helloWorld() {
console.log("Hello, World!");
}
helloWorld();functionkeyword to define a function with a name and then a set of parentheses
function myFunction() {
// Code here
}
myFunction();
console.log(helloWorld()); // Output: Hello, World! π- When defining a function, you can add one or more parameters inside the parentheses to signify what input(s) the function needs when called
function example(parameter) {
// Function code here
}Example
function greetings(name) {
console.log("Hi! My name is " + name + ".");
}
greetings("Marshall");- When calling a function, we call these values arguments that are passed to the function to return something
greetings("Cody");Note: If we do not pass an argument, an undefined value is used instead. (e.g., Hi! My name is undefined.)
Recap:
- Parameters are used when defining a function.
- Arguments are used when calling a function.