2️⃣ JavaScript Basics

🔹 1. Variables

Variables are used to store data.

✅ var

  • Old way to declare variables
  • Function-scoped (not block-scoped)
var name = "Aditya";

✅ let

  • Modern way (recommended)
  • Block-scoped
  • Value can be changed
let age = 25;
age = 30;

✅ const

  • Block-scoped
  • Value cannot be reassigned
const pi = 3.14;

🔹 2. Data Types

📌 String

Text values

let name = "Aditya";

📌 Number

Numbers (integer or decimal)

let marks = 95;
let price = 99.99;

📌 Boolean

True or False

let isLogin = true;

📌 Undefined

Variable declared but not assigned

let x;

📌 Null

Empty value (intentionally)

let data = null;

📌 Object

Collection of key-value pairs

let user = {
name: "Aditya",
age: 25
};

📌 Array

List of values

let fruits = ["Apple", "Banana", "Mango"];

🔹 3. Operators

➕ Arithmetic Operators

Used for calculations

let a = 10;
let b = 5;console.log(a + b); // 15
console.log(a - b); // 5
console.log(a * b); // 50
console.log(a / b); // 2

🔍 Comparison Operators

Compare values (returns true/false)

console.log(10 == "10");   // true (loose)
console.log(10 === "10"); // false (strict)
console.log(10 > 5); // true

🔗 Logical Operators

Used with conditions

let age = 20;console.log(age > 18 && age < 30); // true
console.log(age > 18 || age < 10); // true
console.log(!(age > 18)); // false

📝 Assignment Operators

Assign values

let x = 10;
x += 5; // x = x + 5 → 15
x -= 2; // 13

🔹 4. Type Conversion

📌 Convert to String

let num = 100;
String(num); // "100"

📌 Convert to Number

let str = "50";
Number(str); // 50

📌 Convert to Boolean

Boolean(1); // true
Boolean(0); // false

🧪 Practice Programs

✅ 1. Calculator Program

let a = 10;
let b = 5;console.log("Add:", a + b);
console.log("Subtract:", a - b);
console.log("Multiply:", a * b);
console.log("Divide:", a / b);

👉 Advanced (User Input)

let num1 = Number(prompt("Enter first number:"));
let num2 = Number(prompt("Enter second number:"));alert("Sum = " + (num1 + num2));

✅ 2. Temperature Converter

Celsius to Fahrenheit

let c = 30;
let f = (c * 9/5) + 32;console.log("Fahrenheit:", f);

Fahrenheit to Celsius

let f = 86;
let c = (f - 32) * 5/9;console.log("Celsius:", c);

JavaScript has different types of operators used to perform operations on values and variables. Here’s a complete, clean guide 👇


🔷 1. Arithmetic Operators

Used for mathematical calculations.

OperatorMeaningExample
+Addition5 + 2 = 7
-Subtraction5 - 2 = 3
*Multiplication5 * 2 = 10
/Division5 / 2 = 2.5
%Modulus (remainder)5 % 2 = 1
**Exponentiation2 ** 3 = 8
++Incrementx++
--Decrementx--

🔷 2. Assignment Operators

Used to assign values.

OperatorExampleMeaning
=x = 5Assign
+=x += 2x = x + 2
-=x -= 2x = x - 2
*=x *= 2x = x * 2
/=x /= 2x = x / 2
%=x %= 2x = x % 2
**=x **= 2x = x ** 2

🔷 3. Comparison Operators

Used to compare values (result = true/false).

OperatorExampleResult
==5 == "5"true ⚠️
===5 === "5"false ✅
!=5 != 3true
!==5 !== "5"true
>5 > 3true
<5 < 3false
>=5 >= 5true
<=5 <= 3false

👉 Always prefer === (strict equality)


🔷 4. Logical Operators

Used with boolean values.

OperatorMeaningExample
&&ANDtrue && false → false
||OR`true || false → false
!NOT!true → false

🔷 5. Bitwise Operators

Work on binary numbers.

OperatorExample
&AND
``
^XOR
~NOT
<<Left shift
>>Right shift

🔷 6. String Operators

let name = "Aditya" + " Singh";

👉 Output: "Aditya Singh"


🔷 7. Ternary Operator

Shortcut for if-else.

let result = age >= 18 ? "Adult" : "Minor";

🔷 8. Type Operators

OperatorExample
typeoftypeof "Hello"
instanceofobj instanceof Array

🔷 9. Nullish & Optional Operators (Modern JS)

OperatorMeaning
??Nullish coalescing
?.Optional chaining

Example:

let user = null;
console.log(user?.name); // undefined

🔷 10. Spread & Rest Operators

let arr = [1, 2, 3];
let newArr = [...arr, 4];

🔷 11. Comma Operator

let x = (1, 2, 3);
console.log(x); // 3