JavaScript
1. JavaScript Variables
var name = "Alice";
let age = 20;
const pi = 3.14159;i. var
var is the old way to declare variables in JavaScript.
It is function-scoped, which means it’s available throughout the function where it is defined.
It can also be redeclared and updated.
var name = "Alice";
var name = "Bob"; // Redeclaring is allowed
name = "Charlie"; // Updating is allowed
console.log(name); // Output: Charlieii. let
let is the modern way to declare a variable.
It is block-scoped (only available inside { } where it’s defined).
It can be updated, but cannot be redeclared in the same scope
let age = 20;
age = 21; // Can update value
// let age = 25; // ❌ Cannot redeclare
console.log(age); // Output: 21
if (true) {
let age = 30; // different 'age' (block scope)
console.log(age); // Output: 30
}
console.log(age); // Output: 21iii. const
const pi = 3.1416;
// pi = 3.15; // ❌ Error: Cannot reassign a const variable
console.log(pi); // Output: 3.1416
// But for objects/arrays, properties CAN change:
const user = { name: "Alice" };
user.name = "Bob"; // ✅ Allowed
console.log(user.name); // Output: Bob
iv. JS Datatypes
There are primitive types and non-primitive (object) types
Primitive Types
Primitive values are immutable and stored directly (not by reference).
Type Example Description
| Type | Example | Description |
| ------------- | --------------- | --------------------------------------------------------- |
| **String** | `"Hello"` | Textual data enclosed in quotes. |
| **Number** | `42`, `3.14` | Represents both integers and floating-point numbers. |
| **BigInt** | `123n` | Represents integers of arbitrary length. |
| **Boolean** | `true`, `false` | Logical true/false values. |
| **Undefined** | `undefined` | A variable declared but not assigned a value. |
| **Null** | `null` | Represents an intentional absence of any object value. |
| **Symbol** | `Symbol('id')` | A unique and immutable identifier (used for object keys). |📌 Note: typeof null returns "object" — this is a long-standing JavaScript quirk.
Non-Primitive (Object) Types
Objects are collections of properties (key-value pairs) and are stored by reference.
Get Rijan’s stories in your inbox
Join Medium for free to get updates from this writer.
Example
// Object
const person = { name: "Alice", age: 25 };
console.log(person); // { name: 'Alice', age: 25 }
// Array
const numbers = [1, 2, 3];
console.log(numbers); // [1, 2, 3]
// Function
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Alice")); // Hello, Alice!
// Date
const today = new Date();
console.log(today); // current date and time
// RegExp (Regular Expression)
const pattern = /hello/i;
console.log(pattern.test("Hello World")); // true
// Map
const myMap = new Map();
myMap.set("key1", "value1");
myMap.set("key2", "value2");
console.log(myMap); // Map { 'key1' => 'value1', 'key2' => 'value2' }
// Set
const mySet = new Set();
mySet.add(1);
mySet.add(2);
mySet.add(2); // duplicate ignored
console.log(mySet); // Set { 1, 2 }Type Checking
typeof value→ returns the type as a string
typeof 42; // "number"
typeof "hi"; // "string"
typeof null; // "object" (quirk)
typeof []; // "object"
typeof (() => {}); // "function"Array.isArray([])→ checks if a value is an array.value instanceof Constructor→ checks if an object is an instance of a constructor.
Summary
- 7 primitive types:
string,number,bigint,boolean,undefined,symbol,null - 1 complex type:
object(which includes arrays, functions, and others)
2. JavaScript Operator
In JavaScript (JS), operators are special symbols or keywords used to perform operations on values and variables.
i. Arithmetic Operators
Used to perform basic mathematical operations.
ii. Assignment Operators
Used to assign values to variables.
iii. Comparison Operator
Used to compare two values; return true or false.
iv. Types Operator
Type operators are used to check or determine the data type or object type of a value.
a. typeof Operator
- Purpose: Returns the type of a variable or expression as a string.
- Syntax:
typeof variable- Examples:
typeof "Hello"; // "string"
typeof 42; // "number"
typeof true; // "boolean"
typeof {}; // "object"
typeof undefined; // "undefined"
typeof null; // "object" (special case)
typeof function(){}; // "function"2. instanceof Operator
- Purpose: Checks whether an object is an instance of a particular class or constructor.
- Returns:
trueorfalse. - Syntax:
object instanceof Constructor- Examples:
let arr = [1, 2, 3];
arr instanceof Array; // true
arr instanceof Object; // true
arr instanceof String; // false
let date = new Date();
date instanceof Date; // true


