Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

JavaScript Variables, Data Types, and Operators

Lesson 18 of 34 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

JavaScript Variables, Data Types, and Operators

Variable Declarations

// var — function-scoped, hoisted, avoid in modern JS
var x = 10;

// let — block-scoped, can be reassigned
let count = 0;
count = 5;

// const — block-scoped, cannot be reassigned (but objects can be mutated)
const PI = 3.14;
const user = { name: "Alice" };
user.name = "Bob"; // OK — mutating the object

Data Types

TypeExample
Number42, 3.14, NaN, Infinity
String"hello", 'world', \template\``
Booleantrue, false
Nullnull
Undefinedundefined
SymbolSymbol("id")
BigInt9007199254740991n
Object{}, [], functions
typeof 42;          // "number"
typeof "hello";     // "string"
typeof true;        // "boolean"
typeof null;        // "object" (historical bug)
typeof undefined;   // "undefined"
typeof {};          // "object"
typeof [];          // "object"
typeof function(){}; // "function"

Template Literals

const name = "Alice";
const age = 25;
const msg = `Hello, ${name}! You are ${age} years old.`;
console.log(msg); // Hello, Alice! You are 25 years old.

// Multi-line strings
const html = `
  <div>
    <p>Hello ${name}</p>
  </div>
`;

Operators

// Arithmetic
let a = 10 + 3;   // 13
let b = 10 - 3;   // 7
let c = 10 * 3;   // 30
let d = 10 / 3;   // 3.333...
let e = 10 % 3;   // 1 (modulus)
let f = 2 ** 3;   // 8 (exponent)

// Assignment
x += 5;  x -= 2;  x *= 3;  x /= 2;  x **= 2;

// Comparison
5 == "5"   // true  (loose, type coercion)
5 === "5"  // false (strict, no coercion)
5 != "5"   // false
5 !== "5"  // true
5 > 3      // true

// Logical
true && false  // false (AND)
true || false  // true  (OR)
!true          // false (NOT)

// Nullish coalescing
let val = null ?? "default";  // "default"

// Optional chaining
let city = user?.address?.city;  // undefined (no error)

Type Coercion Examples

"5" + 3    // "53" (string concatenation)
"5" - 3    // 2   (numeric operation)
"5" * 2    // 10
Boolean(0) // false
Boolean("") // false
Boolean([]) // true
Key Takeaway: Use const by default, let when reassignment is needed, and avoid var. Always use === for comparison to avoid type coercion surprises.