javascript


What is a Variable?

An object is a collection of key-value pairs used to store related data and functions.

A variable is a container for storing data. In JavaScript, you use variables to store values that you can use and manipulate throughout your code.



 Declaring Variables

Since ES6, JavaScript has three keywords for declaring variables:


var x = 10;     // Old way (not recommended)
let y = 20;     // Block-scoped, can be reassigned
const z = 30;   // Block-scoped, cannot be reassigned 

JavaScript Data Types

JavaScript is dynamically typed, which means variable types are determined at runtime.


1. Primitive Data Types

1. String – Text data

   
   const name = "Alice";
   

2. Number – Integers and floating-point numbers

   
   let age = 25;
   

3. Boolean – `true` or `false`

   
   const isStudent = true;
   

4. Undefined – A variable declared but not assigned

   
   let score;
   console.log(score); // undefined
   

5. Null – Intentionally empty value

   
   const data = null;
   

6. Symbol – Unique identifier (rarely used)

   
   const id = Symbol("id");
   

7. BigInt – For very large integers

   
   const bigNumber = 12345678901234567890n;
   

 

typeof Operator

Use `typeof` to check the data type of a variable:

console.log(typeof "Hello");   // string
console.log(typeof 123);       // number
console.log(typeof true);      // boolean
console.log(typeof {});        // object
console.log(typeof []);        // object
console.log(typeof null);      // object