JavaScript
JavaScript
Example:
x = 5;
var i = 10;
let y = 6;
const o = 8;
z = x + y;
Block Scope:
● Variables declared inside a { } block cannot be accessed from outside the block.
Global Scope:
● Variables declared with the var always have Global Scope.
● Variables declared with the var keyword can NOT have block scope.
const fruit = {
name: 'apple',
color: 'red',
taste: 'sweet',
};
JavaScript Conditional Statement Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}
Example:
x = 5;
if (x == 5) {
text = 'Hep Hep';
} else {
text = 'Hooray';
} //output Hep Hep
x = 6;
switch (x) {
case 5:
text = 'Hep Hep';
break;
case 6:
text = 'Hooray';
break;
default:
text = 'Hello World';
} //output Hooray
Example:
Example:
const fruit = {
name: 'apple',
color: 'red',
taste: 'sweet',
};
for (key in fruit) {
text += key + ': ' + fruit[key] + ', ';
}
// Output: name: apple, color: red, taste: sweet,