5.1 Number
Number is used for normal numbers in JavaScript. It can store whole numbers and decimal numbers.
Example 1: Whole number
// Store a whole number inside a variable.
let age = 25;
// Show the value in the console.
console.log(age);
// Output:
// 25
Explanation: The variable age stores the number 25.
Example 2: Decimal number
// Store a decimal number.
let price = 19.99;
// Show the price.
console.log(price);
// Output:
// 19.99
Explanation: JavaScript uses Number for decimals too.
5.2 String
String is used for text. Text must be inside quotes.
Example 1: Store text
// Store text inside a variable.
let studentName = "Ali";
// Show the text.
console.log(studentName);
// Output:
// Ali
Explanation: Anything inside quotes is treated as text.
Example 2: Join strings
// Store two strings.
let firstName = "Sara";
let lastName = "Ahmadi";
// Join them together.
console.log(firstName + " " + lastName);
// Output:
// Sara Ahmadi
Explanation: The plus sign can join strings together.
5.3 Boolean
Boolean has only two values: true or false.
Example 1: True value
// Store a true or false value.
let isStudent = true;
// Show the value.
console.log(isStudent);
// Output:
// true
Explanation: Boolean is useful for yes/no situations.
Example 2: Comparison gives Boolean
// Compare two numbers.
let result = 10 > 5;
// Show the answer.
console.log(result);
// Output:
// true
Explanation: Because 10 is greater than 5, the answer is true.
5.4 Undefined
Undefined means a variable exists, but no value has been given yet.
Example 1: Variable without value
// Create a variable but do not give it a value.
let score;
// Show the variable.
console.log(score);
// Output:
// undefined
Explanation: JavaScript says undefined because score has no value yet.
Example 2: Give value later
// Create an empty variable.
let city;
// Give it a value later.
city = "Toronto";
console.log(city);
// Output:
// Toronto
Explanation: A variable can start undefined and receive a value later.
5.5 Null
Null means intentionally empty. The programmer sets it on purpose.
Example 1: Empty user
// No user is selected yet.
let selectedUser = null;
// Show the value.
console.log(selectedUser);
// Output:
// null
Explanation: Null means we purposely have no value right now.
Example 2: Change null later
// Start with no product.
let product = null;
// Later, choose a product.
product = "Laptop";
console.log(product);
// Output:
// Laptop
Explanation: Null can be replaced with a real value later.
5.6 Symbol
Symbol creates a unique value. Even if two Symbols have the same description, they are different.
Example 1: Create Symbol
// Create a unique symbol.
const id = Symbol("id");
// Show the symbol.
console.log(id);
// Output:
// Symbol(id)
Explanation: Symbol is often used for unique object keys.
Example 2: Symbols are unique
// Create two symbols with the same description.
const a = Symbol("key");
const b = Symbol("key");
// Compare them.
console.log(a === b);
// Output:
// false
Explanation: Each Symbol is unique, even if the description is the same.
5.7 BigInt
BigInt is used for very large whole numbers. Add n at the end of the number.
Example 1: BigInt value
// Store a very large integer.
const bigNumber = 999999999999999999999n;
// Show the BigInt.
console.log(bigNumber);
// Output:
// 999999999999999999999n
Explanation: The n tells JavaScript this is a BigInt.
Example 2: BigInt calculation
// Add two BigInt values.
const x = 10n;
const y = 20n;
console.log(x + y);
// Output:
// 30n
Explanation: BigInt can be used for big integer calculations.
5.8 Primitive Types
Primitive types are simple values. They are stored directly.
Example 1: Primitive values
// These are primitive values.
let numberValue = 100;
let stringValue = "Hello";
let booleanValue = true;
console.log(numberValue);
console.log(stringValue);
console.log(booleanValue);
// Output:
// 100
// Hello
// true
Explanation: Number, String, and Boolean are primitive types.
Example 2: Primitive copy
// Store a number.
let a = 5;
// Copy the value of a into b.
let b = a;
// Change b.
b = 10;
console.log(a);
console.log(b);
// Output:
// 5
// 10
Explanation: Primitive values are copied by value.
5.9 Reference Types
Reference types include objects, arrays, and functions. They store a reference to data in memory.
Example 1: Object
// Create an object.
let student = {
name: "Ali",
age: 15
};
// Show the object.
console.log(student);
// Output:
// { name: "Ali", age: 15 }
Explanation: Objects can store many values together.
Example 2: Array
// Create an array.
let colors = ["red", "blue", "green"];
// Show the array.
console.log(colors);
// Output:
// ["red", "blue", "green"]
Explanation: Arrays store lists of values.
5.10 Type Checking
Type checking means finding out what kind of value something is. JavaScript uses typeof for this.
Example 1: Check simple types
// Check different data types.
console.log(typeof 100);
console.log(typeof "Hello");
console.log(typeof true);
// Output:
// number
// string
// boolean
Explanation: typeof tells us the type of a value.
Example 2: Check object and array
// Create an object and an array.
let user = { name: "Sara" };
let numbers = [1, 2, 3];
console.log(typeof user);
console.log(typeof numbers);
console.log(Array.isArray(numbers));
// Output:
// object
// object
// true
Explanation: Arrays show as object with typeof, so use Array.isArray() to check arrays.
Chapter Summary
- Number stores normal numbers like
10 and 19.99.
- String stores text inside quotes.
- Boolean stores
true or false.
- Undefined means no value has been assigned.
- Null means intentionally empty.
- Symbol creates unique values.
- BigInt stores very large whole numbers.
- Primitive types are copied by value.
- Reference types include objects, arrays, and functions.
- Use
typeof and Array.isArray() to check types.