JavaScript has two main categories of data types: Primitives and Reference Types.
Primitives can be copied directly and are stored by value.
- Represents both integers and floating-point numbers
- Special numeric values:
Infinity,-Infinity,NaN
let age = 25;
let price = 99.99;
let infinity = 1 / 0; // Infinity
let notANumber = "hello" / 2; // NaN
console.log(typeof NaN); // "number" (failed mathematical operation)- Represents text data
- Can use single quotes, double quotes, or backticks (template literals)
let name = "John";
let message = "Hello World";
let template = `Hello, ${name}!`; // Template literal- Represents logical values:
trueorfalse
let isActive = true;
let isComplete = false;- Represents an intentional empty value
- It's a primitive but
typeof nullreturns"object"(known JavaScript quirk)
let data = null; // Intentionally empty
console.log(typeof null); // "object" (this is a bug in JavaScript)- Variable declared but not assigned a value
- Default value for uninitialized variables
let x;
console.log(x); // undefined
console.log(typeof x); // "undefined"Reference types cannot be copied directly and are stored as memory references.
- Collection of key-value pairs
- Stores structured data
let user = {
name: "John",
age: 30,
isActive: true,
};- Ordered collection of values
- Special type of object
let numbers = [1, 2, 3, 4, 5];
let mixed = [1, "hello", true, null];- First-class objects in JavaScript
- Can be stored in variables, passed as arguments
function greet() {
console.log("Hello!");
}
let sayHello = function () {
console.log("Hi there!");
};Use the typeof operator to check data types:
console.log(typeof 42); // "number"
console.log(typeof "hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (quirk!)
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function () {}); // "function"Values that evaluate to false in boolean context:
// All falsy values in JavaScript:
false;
0;
(""); // empty string
null;
undefined;
NaN;
document.all; // (legacy, rarely encountered)let value = "hello";
console.log(value); // "hello"
console.log(!value); // false (negation)
console.log(!!value); // true (double negation = actual boolean)
// Double negation (!!) is equivalent to Boolean()
console.log(Boolean(value)); // true
console.log(typeof !!value); // "boolean"// Truthy values (everything not in falsy list)
console.log(!!"hello"); // true
console.log(!!42); // true
console.log(!![]); // true (empty array is truthy)
console.log(!!{}); // true (empty object is truthy)
// Falsy values
console.log(!!0); // false
console.log(!!""); // false
console.log(!!null); // false
console.log(!!undefined); // false
console.log(!!NaN); // falseJavaScript automatically converts types in certain situations:
// String concatenation
console.log("5" + 3); // "53" (number to string)
console.log(5 + "3"); // "53" (number to string)
// Arithmetic operations
console.log("5" - 3); // 2 (string to number)
console.log("5" * 2); // 10 (string to number)
// Comparison
console.log(5 == "5"); // true (loose equality, type coercion)
console.log(5 === "5"); // false (strict equality, no coercion)let a = 5;
let b = a; // b gets a copy of a's value
a = 10;
console.log(b); // 5 (unchanged)let obj1 = { name: "John" };
let obj2 = obj1; // obj2 points to the same object
obj1.name = "Jane";
console.log(obj2.name); // "Jane" (changed because same reference)- Use strict equality (
===) instead of loose equality (==) to avoid unexpected type coercion - Check for
nullexplicitly sincetypeof null === "object" - Use
Array.isArray()to check if something is an array - Be aware of falsy values when writing conditional statements
- Understand the difference between primitives and reference types for proper data handling