A for loop that logs numbers 0 to 4.
for (let i = 0; i < 5; i++) { console.log (1) ; }
- A while loop that decrements x and logs it until x is no longer greater than 0.
let x = 5 while (x > 0) { x—-; console.log (x); }
- Value assignment to variable
const x = -19; const color = 'blue'
- An if...else statement that logs if x is positive or not.
if (x > 0) { console. log("x is positive"); } else 1 console. log("x is zero or negative"); }
- A switch statement that handles different color cases.
switch (color) { case "red": console.log ("Color is red"); break; case "blue": console. log ("Color is blue"); break; default: console. log ("Color is not red or blue"); }
- A try...catch block that handles errors from riskyFunction.
try { let result = riskyFunction(); } catch (error) { console.error(error); } throw new Error ("Something went wrong"); // Throws a new error with a message.
- Literal expressions
42; // The number 42 is a literal expression. "Hello"; // The string "Hello" is a literal expression.
- Arithmetic expressions
5 + 3; // Evaluates to 8. x * y; // Evaluates to the product of x and y.
- Logical expressions
true && false; // Evaluates to false. x || y; // Evaluates to x if x is true, otherwise y.
- Function expressions
const funcExpr = function() & return 42; 3; // Defines a function expression. const arrowFunc = () => 42; // Defines an arrow function expression.
- Object and array initializers
const objectInitializer = { name: "John", age: 30}: // Object initializer expression. const objectInitializer = [11, 2, 31]; // Array initializer expression.
- Property access expressions
obj.name; // Accesses the "name" property of obj.array[0]; // Accesses the first element of array.
- Function calls
square(7); // Evaluates to 49. Math.max(4, 3, 2); // Evaluates to 4;