Variables & Data Types in JavaScript

What is a Variable?

Know more in Detail

A variable is like a container that stores data values. In JavaScript, we use keywords like var, let, and const to declare variables.

Variable Declaration Keywords

// Example
var name = "John"; // old
let age = 25; // modern
const country = "India"; // constant

Data Types in JavaScript

Know more in Detail

JavaScript has different types of data that variables can hold. These are divided into two main categories:

1. Primitive Data Types

Primitive means simple data types that store a single value like a name or number. They are stored directly in memory.

Primitive & Non-primitive in Detail
let name = "Alice"; // String
let age = 30; // Number
let isStudent = true; // Boolean
let score; // Undefined
let empty = null; // Null
let bigNumber = 1234567890123456789012345678901234567890n; // BigInt
let uniqueId = Symbol("id"); // Symbol

2. Non-Primitive (Reference) Data Types

Non-Primitive means complex data types like arrays, objects, and functions. They are stored by reference, which means they point to a memory address.

let person = { name: "Bob", age: 40 }; // Object
let fruits = ["Apple", "Banana"]; // Array
function greet() { // Function console.log("Hello!"); }

Type Checking

You can use typeof to check the data type of a variable:

console.log(typeof "Hello"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"

Summary