JavaScript

Statement

A program, a script, is a series of statements.

A JavaScript program consits of statements, so it is important to learn.

JavaScript statements are composed of values, operators, expressions, keywords and comments.
To separate JavaScript statements, you need to add semicolons.

A JavaScript program is like a little child. You need to explain eveything in details

Block statement

If you want to group statements you can use a block statement. It is delimited by a pair of curly brackets

{ 
statement
}

They are often used with control statements

let y = 10; //variable 
if (y > 1) { //control statement
y--; //decrement
}
console.log(y); //9

Conditional Statements

if (condition){ 
statement 1
}else{
statement 2
};

Conditional statements control behavior in JavaScript and determine whether or not pieces of code can run.If the condition in the if statement is true will it execute. If not it will execute in the else statement.

If statement

The condition must be true for the if statement to be executed.

if(10 > 5) { //this is true if 10 is greater than 5
console.log("10 is bigger than 5");
};
let random = Math.ceil(Math.random() * 6); 
if (random === 1) {
console.log('number 1');
}
if (random === 2) {
console.log('number 2');
}
if (random === 3) {
console.log('number 3');
}
if (random === 4) {
console.log('number 4');
}
if (random === 5) {
console.log('number 5');
}
if (random === 6) {
console.log('number 6');
};

Else statement

Will be executed if the condition is false.

if("cats" === "dogs") { //this is true if cats is identical to dogs
console.log("cats and dogs is the same");
} else {
console.log("cats and dogs are not the same");
};

Else if statement

Will be executed if the first condition is false, but the second is true.

if (false) { 
console.log("if block");
} else if (true) {
console.log("else if block");
}else {
console.log("else block");
};

Logical operators

Logical operators are typically used with boolean and in if else statement.
&& means "and", where ||means "or".

if(10 > 5 && 20 > 10) { 
console.log("This is true");
};

This condition will only be executed if 10 is greatere than 5 AND 20 is greatere than 10

if(10 > 5 || "dogs" === "cats") { 
console.log("This is true");
};

This condition will only be executed if 10 is greatere than 5 OR dog and cats are the same

if("dogs" !== "cats") { 
console.log("This is true");
};

What about this?

let animal = "dogs";
let house = "red";
let weekdays = 7;
let year = 2021;
if(animal === "cats") {
console.log("Cats and dog are the same");
} else if(weekdays === 7 && year === 2022){
console.log("There is 7 days a week and the year is 2022");
} else if(house === "red" || house === "blue"){
console.log("The house is not white");
} else {
console.log("nothing is right");
};

What about this?

Conditional Statements - part 2

Switch Statement

switch (expression) { 
case label_1:
statements_1
[break;]
}

A switch statement allows a program to evaluate an expression and attempt to match expression's value to a case label

let fruits = "apple";
switch(fruits){
case "Banana":
console.log("Banana is good");
break;
case "orange":
console.log("Oranges? That is not me");
break;
default:
console.log("What fruit is that?");
};

Switch expression is evaluated once. WHere the value of the expression is compared with the value og each case.
If there is a match is the code executed, if there is no match the default code is executed.

The break keyword breaks the switch block, which will stop the execution.
The default keyword specifies the code to run if there is no match.