At the end of this class, you will be able to:
true
, false
, 'truth-y', and 'false-y'if/else
conditionals to control program flow based on boolean conditionswilson-espina/jsd-9-resources
repo to your computer:cd
to the Documents/JSD/jsd-9-resources
directorygit pull origin master
and press return
Documents/JSD/jsd-9-resources/04-conditionals-functions
JavaScript always tries to look for truth
{
console.log("hello");
console.log("good bye");
}
if (expression) {
code
}
❌
if (expression) { code }
> | greater than |
>= | greater than or equal to |
< | less than |
=== | strict equal, type and value |
== | coercive equal (AVOID) |
!== | strict not equal, type and value |
!= | coercive not equal (AVOID) |
-1 > 2
//=> false
123 < 6;
//=> false
0.12 >= 0.0023;
//=> true
12 > '12';
//=> false
12 >= '12';
//=> true
'A' > 'a'
//=> false
'b' > 'a';
//=> true
'Glow' > 'Glee';
//=> true
let weather = "sunny";
if (weather === "sunny") {
console.log("Grab your sunglasses");
}
let weather = "sunny";
if (weather === "sunny") {
console.log("Grab your sunglasses");
} else {
console.log("Grab a coat");
}
let weather = "sunny";
if (weather === "sunny") {
console.log("Grab your sunglasses");
} else if (weather === "rainy") {
console.log("Take an umbrella");
} else {
console.log("Grab a coat");
}
(expression) ? trueCode : falseCode;
let drink = (expression) ? trueCode : falseCode;
let sunny = true;
let drink = sunny ? 'beer' : 'coffee';
console.log(drink) // beer
const food = 'apple';
switch(food) {
case 'pear':
console.log('I like pears');
break;
case 'apple':
console.log('I like apples');
break;
default:
console.log('No favourite');
}
//=> I like apples
&& | AND | Returns |
|| | OR | Returns |
! | NOT | Takes a single value and returns the opposite Boolean value |
&&
operator requires both left and right values to be true in order to return true:
true && true
//=> true
true && false
//=> false
false && false
//=> false
||
operator requires just one of the left or right values to be true in order to return true
true || false
//=> true
false || true
//=> true
false || false
//=> false
'use strict';
Open up: 1-conditionals-codealong
All of the following become false when converted to a Boolean:
false
0
'' // (empty string)
NaN
null
undefined
falsy
values because they are equivalent to false
false, 0, "", NaN, null
, and undefined
become true when converted to a Boolean.truthy
values because they are equivalent to true
.
17193
true
'0'
'false'
!!1
//=> true
!!0
//=> false
!![]
//=> true
!!{}
//=> true
!!null
//=> false
!!""
//=> false
Type of Exercise
Location
Timing
15 mins |
|
Code should be written with DRY principals:
Allow us to group a series of statements together to perform a specific task
We can use the same function multiple times
Not always executed when a page loads. Provide us with a way to 'store' the steps needed to achieve a task
function name(parameters) {
// do something
}
function sayHello() {
console.log(“Hello!”);
}
let name = function(parameters) {
// do something
}
let sayHello = function(parameters) {
console.log("Hello!");
}
let name = (parameters) => {
// do something
}
let sayHello = (parameters) => {
console.log("Hello!");
}
function sayGoodbye(){
console.log('good bye');
}
sayGoodbye();
=> good bye
Open up: 3-functions-codealong
Key Objective
Type of Exercise
Location
Timing
4 mins |
|
function newFunc(parameter1, parameter2, parameter3){
console.log(parameter1 + parameter2 + parameter3);
}
The values in the parentheses are placeholders for the data that we pass to our functions and are known as parameters:
function sayHello(name) {
console.log('Hello! ' + name);
}
sayHello('Wilson'); // 'Wilson' is an argument
=> 'Hello Wilson'
function sum(num1, num2) {
console.log(num1 + num2);
}
sum(1, 2); // 1 and 2 are arguments
=> 3
function multiply(num1, num2 = 2) {
console.log(num1 * num2);
}
multiply(4,5);
=> 20
multiply(3);
=> 6 // (argument) * 2 (default value)
Type of Exercise
Location
Timing
3 mins |
|
Type of Exercise
Location
Timing
8 mins |
|
console.log()
is only useful for the developer.
function subtract(num1, num2) {
return num1 - num2;
}
subtract(10,5);
=> 5
function speak(words) {
return words;
// The following code will not run:
let x = 1;
let y = 2;
console.log(x + y)
}
speak('Hey!');
// "Hey!"
Key Objective
Type of Exercise
Location
Timing
10 mins |
|
true
, false
, 'truth-y', and 'false-y'if/else
conditionals to control program flow based on boolean conditionscd
to your Documents/JSD
directory.git clone
and paste the URL and hit return
.(Lesson 04)