Javascript Data Types

  1. Primitive
  2. Non-primitive(reference/object/json)

Primitive

  • Data that is not an object
  • All primitives are immutable i.e., they cannot be altered.
    • Stored in Stack
    • Copies the data(Copy by value)
# There are 7 primitive data types
1. Number
    * Floating point numbers
    * Used for decimals and integers
2. String
    * Sequence of characters
    * Used for text
3. Boolean
    * Logical type that can only be true or false
    * Used for taking decisions
4. Undefined
    * Default value of uninitialized variables
    * You shouldn’t assign undefined as a value manually
5. Null
    * Never assumed by default
    * `reset/clear` a variable using null
6. Symbol (ES2015)
    * Value that is unique and cannot be changed [Not useful for now]
7. BigInt (ES2020)
    * Larger integers than the Number type can hold
    Page: /

Number

  • let n = 123;
  • let Number(num)
  • Besides regular numbers, there are so-called special numeric values
  • Which also belong to that type: Infinity, -Infinity and NaN
* alert( 1 / 0 );       // Infinity
* alert( -1 / 0 );      // -Infinity
* alert( "num"/25 );    // NaN

String

  • Strings are immutable
  • let String(str)
  • In JavaScript, there are 3 types of quotes
* let str = "Hello";  --> Double quotes
* let str2 = 'Hi';    --> Single quotes:
* let phrase = `can ${str}`;      --> Backticks

Boolean (logical type)

  • The boolean type has only two values: true and false
* let Boolean(val)

Symbol

  • “Symbol” value represents a unique identifier.
  • A value of this type can be created using Symbol()
* id is a new symbol
* let id = Symbol();

The null value

  • It’s just a special value which has the sense of nothing, empty or value unknown
* let age = 10;
* age = null;   // reset/clear 

The undefined value

  • If a variable is declared, but not assigned, then its value is exactly undefined
* let x;    // let x = undfined
* alert(x);      -->  shows "undefined"

Bigint

* const b1 = 9007199254740991n
* const b2 = BigInt(9007199254740991)

Primitive wrapper objects in JavaScript

  • Except for null and undefined, all primitive values have object equivalents that wrap around the primitive values:
    • String for the string primitive.
    • Number for the number primitive.
    • BigInt for the bigint primitive.
    • Boolean for the boolean primitive.
    • Symbol for the symbol primitive.

Non-primitive

  • Object, Array, RegExp
  • Stored in Heap
  • Copies the pointer(Copy by reference)
// REFERENCE TYPES - Objects

// Array..The arrays are objects,
const hobbies = ['movies', 'music'];

// Object literal
const address = {
  city: 'Boston',
  state: 'MA'
}
const today = new Date();
console.log(today);

// Regular Expressions
const re1 = /\w+/;
const re2 = new RegExp('\\w+');
console.log(re2);

//Typeof
console.log(typeof today);  
console.log(typeof address)

Garbage Collection

  • Memory management in JavaScript is performed automatically and invisibly to us.
  • We create primitives, objects, functions… All that takes memory.
  • Garbage collector monitors all objects and removes those that have become unreachable.
    • i.e those variable that are not used anywhere

Memory leaks

  • Referencing something that is not in anyuse leads to memory leak
  • Those memory cannot be freed

Reference