36
loading...
This website collects cookies to deliver better user experience
if (condition) {
// run here is condition is true
} else {
// run here is condition is false
}
const myMum = "Maria";
if (typeof myMum === 'string') {
console.log("My mum contains a string");
} else {
console.log("My mum is not a string");
}
const
is a string
and run the following sentences according to the condition, obviously this condition is true
, the you will see My Mum contains a string
in your console
😇. logic operators
, AND &&
and OR ||
. &&
: It means that all the conditions must be true due to true
of the entire condition.
// It returns true due to both sides are true.
true===true
||
: It means that at least one of the conditions must be true to get a final true
:
// it returns true due to at least one side is true
true || false
if
clause 🤖const myMum = "Maria";
if (true && typeof myMum === 'string') {
console.log("My mum contains a string");
} else {
console.log("My mum is not a string");
}
My mum contains a string
because two conditions are true and I have used an AND logic port. switch (condition) {
case valor1:
// It will run when the conditions is match `valor1`
[break;]
case valor2:
// It will run when the conditions is match `valor2`
[break;]
...
case valorN:
// It will run when the conditions is match `valorN`
[break;]
default:
// It will run when all the conditions are false
[break;]
}
if-else
clauses, but your code will not be legible 🤒. It comes to bring us more organisation when we need to check conditions that can take a lot different values, Let's see an example:const foo = 0;
switch (foo) {
case -1:
console.log('1 negative');
break;
case 0: // foo is 0, then the following block will be run
console.log(0)
break; // Break allow us not to run case1
case 1:
console.log(1);
break; // Break allow us not to run case2
case 2:
console.log(2);
break;
default:
console.log('default');
}
const animal = 'giraffe';
switch (Animal) {
case 'Dog':
case 'giraffe':
case 'Cat':
case 'Bird':
console.log('This animal will live.');
break;
case 'elephant':
default:
console.log('This animal will not.');
}