JavaScript supports various operators for arithmetic, comparison, and logical operations:
let a =5;let b =10;// Arithmetic operatorslet sum = a + b;let difference = a - b;let product = a * b;let quotient = a / b;// Comparison operatorslet isEqual = a === b;let isNotEqual = a !== b;let greaterThan = a > b;let lessThan = a < b;// Logical operatorslet andOperator =true&&false;let orOperator =true||false;let notOperator =!true;
3. Control Flow
Use if, else if, and else statements for control flow:
let x =10;if (x >0) {console.log("Positive");} elseif (x <0) {console.log("Negative");} else {console.log("Zero");}
4. Loops
JavaScript supports for and while loops:
// For loopfor (let i =0; i <5; i++) {console.log(i);}// While looplet j =0;while (j <5) {console.log(j); j++;}
5. Functions
Define functions using the function keyword:
functiongreet(name) {console.log("Hello, "+ name +"!");}greet("John");
6. Arrays and Objects
Arrays and objects are crucial data structures in JavaScript: