- Single-line Comment:
Use//
to comment out a single line of code.// This is a single-line comment let x = 10; // This is also a comment
- Multi-line Comment:
Use/* ... */
to comment out multiple lines of code./* This is a multi-line comment */ let y = 20;
Operators are symbols that perform operations on variables and values.
Types of Operators:
-
Arithmetic Operators:
- Used for mathematical calculations.
- Examples:
let a = 10; let b = 5; console.log(a + b); // Addition: 15 console.log(a - b); // Subtraction: 5 console.log(a * b); // Multiplication: 50 console.log(a / b); // Division: 2 console.log(a % b); // Modulus: 0 (remainder)
-
Assignment Operators:
- Used to assign values to variables.
- Examples:
let x = 10; // Assign 10 to x x += 5; // x = x + 5 (x becomes 15) x -= 2; // x = x - 2 (x becomes 13)
-
Comparison Operators:
- Used to compare two values.
- Examples:
let a = 10; let b = 20; console.log(a == b); // Equal to: false console.log(a != b); // Not equal to: true console.log(a > b); // Greater than: false console.log(a < b); // Less than: true
-
Logical Operators:
- Used to combine two or more conditions.
- Examples:
let a = 10; let b = 20; console.log(a > 5 && b > 15); // Logical AND: true console.log(a > 15 || b > 15); // Logical OR: true console.log(!(a > 15)); // Logical NOT: true
-
Increment/Decrement Operators:
- Used to increase or decrease the value of a variable by 1.
- Examples:
let count = 5; count++; // Increment: count becomes 6 count--; // Decrement: count becomes 5
-
Ternary Operator:
- A shorthand for if-else statements.
- Example:
let age = 18; let result = (age >= 18) ? "Adult" : "Minor"; console.log(result); // Output: "Adult"
Conditional statements are used to perform different actions based on different conditions.
-
if
Statement:- Executes a block of code if the specified condition is true.
- Example:
let age = 18; if (age >= 18) { console.log("You are eligible to vote."); }
-
if-else
Statement:- Executes one block of code if the condition is true, and another if it is false.
- Example:
let age = 16; if (age >= 18) { console.log("You are eligible to vote."); } else { console.log("You are not eligible to vote."); }
-
if-else if-else
Statement:- Executes multiple blocks of code depending on different conditions.
- Example:
let marks = 85; if (marks >= 90) { console.log("Grade A"); } else if (marks >= 75) { console.log("Grade B"); } else if (marks >= 50) { console.log("Grade C"); } else { console.log("Fail"); }