In JavaScript, data types and variables are fundamental concepts that you’ll frequently encounter when coding. Here’s a breakdown of the basic data types, variables, and how they are used, with examples:
Data Types
JavaScript has several basic data types:
1. Primitive Data Types:
- Number: Represents both integer and floating-point numbers.
let age = 25; // Integer
let temperature = 98.6; // Floating-point number- String: Represents a sequence of characters.
let name = "John Doe";
let greeting = 'Hello, World!';- Boolean: Represents a value of either
trueorfalse
let isActive = true;
let isCompleted = false;- Undefined: Represents a variable that has been declared but not yet assigned a value.
let user; // undefined- Null: Represents the intentional absence of any object value
let emptyValue = null;- Symbol: Represents a unique and immutable value used as an object property key.
const uniqueID = Symbol('id');- BigInt: Represents integers with arbitrary precision
let largeNumber = 1234567890123456789012345678901234567890n;2. Object Data Types:
- Object: Represents a collection of properties, where each property is a key-value pair.
let person = {
firstName: "Jane",
lastName: "Doe",
age: 30
};- Array: A special type of object used for storing ordered collections of values
let numbers = [1, 2, 3, 4, 5];
let colors = ["red", "green", "blue"];- Function: A type of object that represents executable code
function greet(name) {
return `Hello, ${name}!`;
}Variables
Variables in JavaScript are used to store data that can be referenced and manipulated. You can declare variables using var, let, or const:
1. var:
- Oldest way to declare variables, function-scoped.
var x = 10;- Can lead to issues due to hoisting and scoping quirks.
2. let:
- Block-scoped, introduced in ES6 (ECMAScript 2015).
let y = 20;- Useful for variables that change.
3. const:
- Block-scoped, but the variable’s value cannot be reassigned after initialization.
const z = 30;- Best for constants and values that shouldn’t change.
Examples
1. Basic Variable Usage:
let name = "Alice";
let age = 28;
const pi = 3.14159;
console.log(name); // Output: Alice
console.log(age); // Output: 28
console.log(pi); // Output: 3.141592. Using Different Data Types:
let isRaining = true; // Boolean
let temperature = 72.5; // Number (float)
let city = "San Francisco"; // String
let data = [1, 2, 3, 4]; // Array
let person = { name: "Bob", age: 40 }; // Object
console.log(isRaining); // Output: true
console.log(temperature); // Output: 72.5
console.log(city); // Output: San Francisco
console.log(data); // Output: [1, 2, 3, 4]
console.log(person.name); // Output: Bob3. Function Example:
function add(a, b) {
return a + b;
}
let sum = add(5, 10);
console.log(sum); // Output: 15Summary
- Primitive data types are basic and immutable.
- Object data types include objects, arrays, and functions, and are mutable.
- Variables store data and can be declared using
var,let, orconst.
Understanding these data types and variable declarations is fundamental to start writing JavaScript programs.

