REVIEW
| String | Represents textual data | 'hello', "hello", `hello` |
| Number | An integer or floating-point number | 3, -3, 0.1234, 3e-9 |
| Boolean | Either true or false | true or false |
| null | value is empty or nothing | |
| undefined | uninitialized value |
Conditional Logic
Conditional Logic
We can use if statements in JavaScript to make decisions based on certain conditions.
if (condition is true) {
// run this code
}
Conditional Logic
Conditional Logic
When working with conditionals, we utilize comparison operators that either returns true or false.
| == | equality |
| === | strict equality |
| != | inequality |
| !== | strict inequality |
| > | greater than |
| >= | greater than or equal to |
| < | less than |
| <= | less than or equal to |
Conditional Logic
==)Compares the value on the left to the right and returns true if the values are equivalent or false if they are not.
console.log(10 == 10); // returns true
console.log('hello' == 'bye') // returns false
console.log('10' == 10); // returns true
Note: The == operator does not care about types when comparing data. JavaScript will convert one type to another. This is called Type Coercion.
Conditional Logic
===)Compares the value on the left to the right and returns true if the data types AND values are equivalent or false if they are not.
console.log(10 === 10); // returns true
console.log('hello' === 'bye') // returns false
console.log('10' === 10); // returns false
Conditional Logic
!=)Compares the value on the left to the right and returns true if the values are NOT equivalent or false if they are.
console.log(10 != 10); // returns false
console.log('hello' != 'bye') // returns true
console.log('10' != 10); // returns false
Conditional Logic
!==)Compares the value on the left to the right and returns true if the data types AND values are NOT equivalent or false if they are.
console.log(10 !== 10); // returns false
console.log('hello' !== 'bye') // returns true
console.log('10' !== 10); // returns true
Conditional Logic
>)Compares two numbers. It returns true if the number on the left is greater than the number on the right. Otherwise, it returns false.
console.log(10 > 2); // returns true
console.log(2 > 3) // returns false
console.log(10 > 10); // returns false
console.log('10' > 8); // returns true
console.log('hello' > 1) // return falseConditional Logic
>=)Compares two numbers. It returns true if the number on the left is greater than or equal to the number on the right. Otherwise, it returns false.
console.log(10 >= 2); // returns true
console.log(2 >= 3) // returns false
console.log(10 >= 10); // returns true
console.log('10' >= 8); // returns true
Conditional Logic
<)Compares two numbers. It returns true if the number on the left is less than the number on the right. Otherwise, it returns false.
console.log(10 < 2); // returns false
console.log(2 < 3) // returns true
console.log(10 < 10); // returns false
console.log('10' < 8); // returns false
Conditional Logic
<=)Compares two numbers. It returns true if the number on the left is less than or equal to the number on the right. Otherwise, it returns false.
console.log(10 <= 2); // returns false
console.log(2 <= 3) // returns true
console.log(10 <= 10); // returns true
console.log('10' <= 8); // returns false
Conditional Logic
When working with conditionals, we can use logical operators to check multiple conditions at the same time.
| && | logical AND operator |
| || | logical OR operator |
Conditional Logic
Returns true if and only if the left and right operands are true. Otherwise, returns false.
// Instead of this
if (age >= 13) {
if (age < 18) {
console.log('teenager')
}
}
// Use the && operator
if (age >= 13 && age < 18) {
console.log('teenager')
}
Conditional Logic
||)Returns true if either the left or right operands is true. Otherwise, returns false.
const role = 'mom';
// Instead of this
if (role === 'mom') {
console.log('parent');
}
if (role === 'dad') {
console.log('parent');
}
// Use the || operator
if (role === 'mom' || role === 'dad') {
console.log('parent')
}
Conditional Logic
We can chain else if and else statements after an if statement to check for alternate conditions.
if (num > 15) {
console.log("Bigger than 15");
} else if (num < 5) {
console.log("Smaller than 5");
} else if (num === NaN) {
console.log('num is not a number!')
} else {
console.log("Between 5 and 15");
}
Conditional Logic
Code is executed from top to bottom so you must be careful of which statement comes first!
if (num < 1) {
console.log('Less than one');
} else if (num < 2) {
console.log('Less than two');
} else {
console.log('Greater than or equal to 2');
}
if (num < 2) {
console.log('Less than two');
} else if (num < 1) {
console.log('Less than one');
} else {
console.log('Greater than or equal to 2');
}
Loops
Loops are used in JavaScript to execute the same piece of code over and over again until a condition is met to stop it.
There are three key parts of a loop:
If your loop is missing any of these, you will run into an infinite loop which can cause your browser to crash or freeze.
Loops
For example, if you want to print the numbers 0-5:
console.log(0);
console.log(1);
console.log(2);
console.log(3);
console.log(4);
console.log(5);Loops
...but what if you want to print the numbers 0-100?
console.log(0);
console.log(1);
console.log(2);
console.log(3);
console.log(4);
console.log(5);
// yeah noLoops
We can use a for loop to run a block of code a specific number of times.
for (start index; stop condition; index count) {
// code to be executed at each iteration
}
false.Loops
We can use a for loop to run a block of code a specific number of times.
for (let i = 0; i <= 100; i++) {
console.log(i);
}
Loops
We don't have to iterate one at a time. We can change the index counter statement to any expression.
for (let i = 0; i <= 100; i += 2) {
console.log(i);
}
Concept Practice: Loops
Print all odd numbers between 1 to 99 to the console.
Concept Practice: Loops
Print all odd numbers between 1 to 99 to the console.
for (let i = 1; i <= 99; i += 2) {
console.log(i);
}
Concept Practice: Loops
Print the numbers 1 to 9 backwards.
Concept Practice: Loops
Print the numbers 1 to 9 backwards.
for (let i = 9; i > 0; i--) {
console.log(i);
}
Concept Practice: Loops
Print out the sum of all numbers between 0 to 100.
Concept Practice: Loops
Print out the sum of all numbers between 0 to 100.
let sum = 0;
for (let i = 0; i <= 100; i++) {
sum += i;
}
console.log(sum);
Concept Practice: Loops
For numbers between 1 to 100, print "Fizz" if the number is divisible by 3; print "Buzz" if the number is divisible by 5; print "FizzBuzz" if the number is divisible by 3 and 5; and print the number if not of the above applies.
Concept Practice: Loops
For numbers between 1 to 100, print "Fizz" if the number is divisible by 3; print "Buzz" if the number is divisible by 5; print "FizzBuzz" if the number is divisible by 3 and 5; and print the number if not of the above applies.
for (let i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log('FizzBuzz');
} else if (i % 3 == 0) {
console.log('Fizz');
} else if (i % 5 == 0) {
console.log('Buzz');
} else {
console.log(i);
}
}
Concept Practice: Loops
More practice sets to test your algorithm and problem-solving skills. Some of these are common interview questions!
Loops
Loops are very commonly used with arrays. We'll learn about arrays next session so stay tuned!
Functions
alert('some string')prompt('some string')console.log('some string')Functions
function name() {
// code to be executed
}
A function is defined using the function keyword followed by:
{}, which contains code statements that are to be executed when the function is calledFunctions
function sayHelloWorld() {
console.log("Hello World!");
}
function getTodaysDate() {
const date = new Date();
return date;
}
Functions
Once the function is defined, we can invoke the function anywhere in our code by calling its name with the pair of parentheses, for example:
function sayHelloWorld() {
console.log("Hello World!");
}
// call the function
sayHelloWorld();
Functions
function getTodaysDate() {
const date = new Date();
return date;
}
const today = getTodaysDate();
console.log(today);
Functions can have a return statement that sends a value back out of a function. We can store this value in another variable or use it as a value in another function.
Functions
function testFn(parameter1, parameter2, parameter3) {
console.log(parameter1);
console.log(parameter2);
console.log(parameter3);
}
testFn('hello', 'world', 99);
testFn('abc', 99, false)
Parameters are variables that act as placeholders for the values that are to be input to a function when it is called. The actual values that are passed are called arguments.
Functions
function sayHelloTo(personName) {
const greeting = "Hello " + personName;
console.log(greeting);
}
function sayHelloToFrom(personName, fromPerson) {
const greeting = "Hello " + personName + " from " + fromPerson;
console.log(greeting);
}
sayHelloTo('Bob');
sayHelloToFrom('Bob', 'Jane');
Functions
A function's name should describe what the function does and its name typically consists an action, for example
get... to get and return a valuecalc... to calculate valuescreate... to create somethingcheck... to check something and return a booleanon... to describe an interaction on a target elementFunctions
A function's name should describe what the function does and its name typically consists an action, for example
getPhoneNumber(); calcAverage(); createLogin();checkPermission(); onClick(); Concept Practice: Functions
Write a function that returns the sum of two numbers. Print the result to the console.
Concept Practice: Functions
Write a function that returns the sum of two numbers. Print the result to the console.
function getSum(numA, numB) {
return numA + numB;
}
let sum = getSum(4, 7);
console.log(sum);
Concept Practice: Functions
Write a function that checks if a number is odd or even. If odd, print "odd" to the console. Otherwise, print "even".
Concept Practice: Functions
Write a function that checks if a number is odd or even. If odd, print "odd" to the console. Otherwise, print "even".
function checkEvenOrOdd(num) {
if (num % 2 === 0) {
console.log("even");
} else {
console.log("odd");
}
}
checkEvenOrOdd(2);
Concept Practice: Functions
Write a function that takes in two numbers. Return the sum of all numbers between the two given numbers and print the result to the console.
Concept Practice: Functions
Write a function that takes in two numbers. Return the sum of all numbers between the two given numbers and print the result to the console.
function sum(numA, numB) {
let sum = 0;
for (let i = numA; i <= numB; i++) {
sum += i;
}
return sum;
}
console.log(sum(1, 3));
console.log(sum(1, 100));
Arrays
Objects