


Khusboo Tayal
JavaScript is a dynamically typed language, which means you do not need to specify the data type of a variable when you declare it. In this chapter, we will explore how variables work, the different data types available, and the various operators you can use in JavaScript.
Variables are containers used to store data values. In JavaScript, you can declare variables using three keywords: var, let, and const.
// Using var (Old method, function-scoped) var name = "John"; // Using let (Modern, block-scoped) let age = 25; // Using const (Constant, block-scoped) const pi = 3.14159;
JavaScript has seven primary data types, which can be categorized as Primitive Types and Non-Primitive Types.
let text = "Hello, World!";
let number = 42; let price = 99.99;
let isActive = true;
let value; console.log(value); // Output: undefined
let empty = null;
let uniqueId = Symbol("id");let largeNumber = 12345678901234567890n;
let person = {
name: "Alice",
age: 30,
isActive: true
};
let fruits = ["Apple", "Banana", "Cherry"];
function greet() {
console.log("Hello, World!");
}
JavaScript is a dynamically typed language, meaning the type of a variable can change during runtime.
let data = 42; // data is a Number data = "Hello"; // data is now a String data = true; // data is now a Boolean
Operators are symbols that perform operations on values or variables. JavaScript has several types of operators:
let a = 10; let b = 3; console.log(a + b); // Addition (Output: 13) console.log(a - b); // Subtraction (Output: 7) console.log(a * b); // Multiplication (Output: 30) console.log(a / b); // Division (Output: 3.333) console.log(a % b); // Modulus (Remainder) (Output: 1) console.log(a ** b); // Exponentiation (Output: 1000)
let x = 5; x += 2; // Same as x = x + 2 x -= 1; // Same as x = x - 1 x *= 3; // Same as x = x * 3 x /= 2; // Same as x = x / 2
These are used to compare two values. console.log(10 > 5); // Greater than (true) console.log(10 < 5); // Less than (false) console.log(10 == 5); // Equal to (false) console.log(10 === "10"); // Strictly equal (false) console.log(10 !== 5); // Not equal (true)
These are used to perform logical operations.
let isAdult = true; let hasPermission = false; console.log(isAdult && hasPermission); // AND (false) console.log(isAdult || hasPermission); // OR (true) console.log(!isAdult); // NOT (false)
A compact way of writing conditional statements.
let age = 18; let canVote = (age >= 18) ? "Yes" : "No"; console.log(canVote); // Output: Yes
console.log(typeof 42); // "number" console.log(typeof "Hello"); // "string" console.log(typeof true); // "boolean"
let date = new Date(); console.log(date instanceof Date); // true
JavaScript has three main types of scope:
let globalVar = "I am global";
function testScope() {
let localVar = "I am local";
if (true) {
let blockVar = "I am block-scoped";
console.log(blockVar); // Accessible here
}
// console.log(blockVar); // Error: blockVar is not defined
}
testScope();