1. String concatenation
console.log('my' + 'cat');
console.log('1' + 2);
console.log(`string literals: 1 + 2 = ${1 + 2}`);
2. Numeric operators
console.log(1 + 1);
console.log(1 - 1);
console.log(1 / 1);
console.log(1 * 1);
console.log(5 % 2);
console.log(2 ** 3);
3. Increment operators
let counter = 2;
const preIncrement = ++counter;
console.log(`preIncrement : ${preIncrement}, counter : ${counter}`);
const postIncrement = counter++;
console.log(`postIncrement : ${preIncrement}, counter : ${counter}`);
4. Assignment operators
let x = 3;
let y = 6;
x += y;
x -= y;
x *= y;
x /= y;
5. Comparison operators
console.log(10 < 6);
console.log(10 <= 6);
console.log(10 > 6);
console.log(10 >= 6);
6. Logical perators: || or , && and, ! not
const value1 = false;
const value2 = 4 < 2;
console.log(`or: ${value1 || value2 || check()}`);
console.log(`and: ${value1 && value2 && check()}`);
function check(){
for (let i = 0; i < 10; i++){
console.log('yay!')
}
return true;
}
console.log(!value1);
7.Equality
const stringFive = "5";
const numberFive = 5;
console.log(stringFive == numberFive);
console.log(stringFive != numberFive);
console.log(stringFive === numberFive);
console.log(stringFive !== numberFive);
const somi1 = { name: 'somi' };
const somi2 = { name: 'somi' };
const somi3 = somi1;
console.log(somi1 == somi2);
console.log(somi1 === somi2);
console.log(somi1 === somi3);
quiz
console.log(0 == false);
console.log(0 === false);
console.log('' == false);
console.log('' === false);
console.log(null == undefined);
console.log(null === undefined);
8. Conditional operators: if
const name = 'somi';
if (name === 'somi') {
console.log('welcome Somi');
} else if (name === 'coder') {
console.log('you are an amazing coder');
} else {
console.log('unknown');
}
9. Ternary operator: ?
console.log(name === 'somi' ? 'yes' : 'no');
10. Switch statement
const browser = 'Chrome';
switch (browser) {
case 'IE':
console.log('go away!');
break;
case 'Chrome':
case 'Firefox':
console.log('love you!');
break;
default:
console.log('same all');
break;
}
11. Loops
let i = 3;
while (i > 0) {
console.log(`while: ${i}`);
i--;
}
do {
console.log(`do while: ${i}`);
i--;
}while (i > 0);
for (i = 3; i > 0; i--) {
console.log(`for: ${i}`);
}
for (let i = 3; i > 0; i = i - 2) {
console.log(`inline variable for : ${i}`)
}
for (let i = 0; i < 10; i++) {
for (let j = 0; j <10; j++) {
console.log(`i: ${i}, j: ${j}`)
}
}
quiz
for (let i = 0; i < 11; i++) {
if (i % 2 !== 0) {
continue;
}
console.log(`q1. ${i}`)
};
for ( let i = 0; i < 11; i++) {
if (i > 8) {
break;
}
console.log(`q2. ${i}`)
}