JavaScript Data Types

Program demonstrating different data types in JavaScript

JavaScriptBeginner
JavaScript
// Primitive Data Types
console.log("=== Primitive Types ===");

// Number
let age = 25;
let price = 99.99;
console.log("Number:", age, typeof age);
console.log("Float:", price, typeof price);

// String
let name = "John";
let message = 'Hello World';
let template = `Name: ${name}`;
console.log("String:", name, typeof name);
console.log("Template Literal:", template);

// Boolean
let isActive = true;
let isComplete = false;
console.log("Boolean:", isActive, typeof isActive);

// Undefined
let undefinedVar;
console.log("Undefined:", undefinedVar, typeof undefinedVar);

// Null
let nullVar = null;
console.log("Null:", nullVar, typeof nullVar);

// Symbol (ES6)
let sym = Symbol("id");
console.log("Symbol:", sym, typeof sym);

// BigInt (ES2020)
let bigNumber = 9007199254740991n;
console.log("BigInt:", bigNumber, typeof bigNumber);

// Reference Types
console.log("\n=== Reference Types ===");

// Object
let person = {
    name: "John",
    age: 25
};
console.log("Object:", person, typeof person);

// Array
let numbers = [1, 2, 3, 4, 5];
console.log("Array:", numbers, typeof numbers);

// Function
function greet() {
    return "Hello";
}
console.log("Function:", greet, typeof greet);

Output

=== Primitive Types ===
Number: 25 number
Float: 99.99 number
String: John string
Template Literal: Name: John
Boolean: true boolean
Undefined: undefined undefined
Null: null object
Symbol: Symbol(id) symbol
BigInt: 9007199254740991n bigint
=== Reference Types ===
Object: { name: 'John', age: 25 } object
Array: [ 1, 2, 3, 4, 5 ] object
Function: [Function: greet] function

This program demonstrates all data types available in JavaScript.

Primitive Data Types

Primitives are immutable values stored directly in memory:

  1. Number: Integers and floating-point numbers

    • typeof 42"number"
    • Includes Infinity, -Infinity, NaN
  2. String: Text data

    • Single quotes: 'text'
    • Double quotes: "text"
    • Template literals: text
  3. Boolean: true or false

    • Used in conditionals and logic
  4. Undefined: Variable declared but not assigned

    • let x; console.log(x);undefined
  5. Null: Intentional absence of value

    • typeof null"object" (historical bug)
  6. Symbol: Unique identifier (ES6)

    • Used for object property keys
  7. BigInt: Large integers (ES2020)

    • Append n to number: 9007199254740991n

Reference Data Types

Stored as references, not values:

  1. Object: Key-value pairs

    javascript
    { name: "John", age: 25 }
    
  2. Array: Ordered list

    javascript
    [1, 2, 3, 4, 5]
    
  3. Function: Callable code block

    javascript
    function greet() { return "Hello"; }
    

Type Checking

Use typeof operator to check types:

javascript
typeof 42;        // "number"
typeof "text";    // "string"
typeof true;      // "boolean"
typeof undefined; // "undefined"
typeof null;      // "object" (bug!)
typeof {};        // "object"
typeof [];        // "object"
typeof function(){}; // "function"

Important Notes:

  • JavaScript is dynamically typed
  • Variables can change types
  • Use === for strict equality checks
  • typeof null returns "object" (historical bug)