Khachatryan-dev

JavaScript Types

JavaScript Types: The Complete Guide

Aram
Khachatryan

JavaScript Types: A Complete Guide


JavaScript uses dynamic typing, where the type of a variable is automatically determined based on the value it holds. Types are divided into two main categories: primitive (value-based) and object (reference-based).

1. Primitive Types


1.1 Number


Represents numeric values, either integer or decimal.


Example:


    let age = 25; // integer
    let price = 9.99; // decimal

Special values:


  • NaN (Not a Number): Occurs during invalid mathematical operations.
  • Infinity and -Infinity: Represent infinity.

Read more here




1.2 String


Textual data enclosed in single (''), double ("") quotes, or backticks (``).


Example:


    let name = "Anna";
    let message = `Hello, ${name}!`; // template string

Read more here




1.3 Boolean


Has two values: true (truthy) and false (falsy).


Example:


    let isOnline = true;
    let hasPermission = false;

Read more here




1.4 Null


Represents an empty or unknown value. Must be explicitly assigned.


Example:


    let user = null; // user has no value

Read more here




1.5 Undefined


Indicates a variable is declared but not assigned a value.


Example:


    let score;
    console.log(score); // undefined

Read more here




1.6 Symbol (ES6+)


A unique value used for object property keys.


Example:

    const id = Symbol("id");



1.7 BigInt (ES2020+)


Represents large integers by appending n.


Example:


    const bigNumber = 123456789012345678901234567890n;



2. Object Types


Objects store complex data structures and are referenced by their memory location.


2.1 Object


A collection of key-value pairs.


Example:


    let person = {
      name: "Karen",
      age: 30
    };



2.2 Array


An ordered collection of elements accessible by index.


Example:


    let colors = ["red", "blue", "green"];



2.3 Function


A reusable block of code that can be invoked.


Example:


    function add(a, b) {
      return a + b;
    }



2.4 Other Objects


  • Date: Handles dates and times.
  • RegExp: Regular expressions.
  • Error: Error handling.

Many other object types exist.




3. Type Checking


typeof


The typeof operator returns the type as a string.


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

instanceof


instanceof checks if an object belongs to a specific class.


    console.log([] instanceof Array); // true



4. Type Coercion


JavaScript automatically converts types during operations:


  • Number → String: 5 + "" → "5"
  • String → Number: +"10" → 10
  • Boolean conversion: !!0 → false, !!"text" → true



5. Memory Management


JavaScript manages memory differently based on types:


5.1 Primitive Types (Value-Based)


  • Stored in the stack as independent values.
  • Each variable has its own copy.

    let a = 10;
    let b = a; // Copies the value
    a = 20;
    console.log(b); // 10 (unchanged)

5.2 Object Types (Reference-Based)


  • Stored in the heap with memory references.
  • Variables store memory addresses.

    let obj1 = { name: "Anna" };
    let obj2 = obj1; // Copies the reference
    obj1.name = "Karen";
    console.log(obj2.name); // "Karen" (modified)



6. Best Practices


  • Use === (strict equality) instead of == to avoid type coercion.
  • Check for NaN with isNaN().
  • Declare variables using let and const.
5.00 / 2
Buy me a coffee
  • 0
  • 18

Discover More Content

Comments
No data
No comments yet. Be the first to comment!
Leave a Comment
You must be logged in to leave a comment.Login