DEV Community

Himanshu Gupta
Himanshu Gupta

Posted on • Edited on

Primitive vs Reference Data Types in JavaScript.

Primtive data types in JavaScript
primitive data types value in the call *stack *(Execution context).

  1. String: Represents a sequence of characters, such as a word or a sentence. You can create a string in JavaScript by enclosing a sequence of characters in quotes (either single or double quotes). For example:
    let name = "John Doe";
    let greeting = 'Hello, World!';

  2. Number: Represents numeric values, such as integers or floating-point numbers. There is only one Number type in JavaScript and it can represent both integers and floating-point numbers. For example:
    let age = 30;
    let height = 1.75;

03.Boolean: Represents a value that can either be true or false. For example:
let isValid = true;
let isFinished = false;

  1. Null: Represents an intentional non-value. It is often used to indicate that an object reference does not refer to an instance of an object. For example:
    let person = null;

  2. Undefined: Represents an absence of a value. A variable that has been declared but has not been assigned a value will have the value undefined. For example:
    let name;
    console.log(name); // Output: undefined

  3. A Symbol is a unique and immutable data type that can be used as an identifier for object properties. It was introduced in ECMAScript 6 as a way to create unique property keys, which are used to prevent property name collisions in complex object-oriented programs.
    `let id = Symbol();
    let anotherId = Symbol();

console.log(id === anotherId); // Output: false`

  1. Bigint is a primitive data type that was introduced in ECMAScript 2020. It is represented by the keyword "bigint" and can be used to store integers of arbitrary size, similar to other numeric data types such as "number". However, unlike "number", "bigint" can represent integers with arbitrary precision, without losing accuracy.
const bigNum = 1234567890123456789012345678901234567890n;
console.log(bigNum); // Output: 1234567890123456789012345678901234567890n

const anotherBigNum = BigInt("9876543210987654321098765432109876543210");
console.log(anotherBigNum); // Output: 9876543210987654321098765432109876543210n

const result = bigNum * anotherBigNum;
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Reference data types in JavaScript
The size of a reference data type value is dynamic therefore It is stored on Heap

  1. Object: Represents a collection of properties, where each property has a name and a value. Properties can be accessed using dot notation (e.g. object.property) or square bracket notation (e.g. object['property']). You can create an object using the object literal syntax ({}) or the Object constructor. For example: `// Object literal syntax let person = { name: "John Doe", age: 30 };

// Object constructor
let anotherPerson = new Object();
anotherPerson.name = "Jane Doe";
anotherPerson.age = 25;`

  1. Array: Represents a collection of values that can be of any data type, including other objects and arrays. You can access individual values in an array using an index (e.g. array[index]). You can create an array using the array literal syntax ([]) or the Array constructor. For example: `// Array literal syntax let numbers = [1, 2, 3, 4, 5];

// Array constructor
let anotherNumbers = new Array(6, 7, 8, 9, 10);`

  1. Function: Represents a block of code that can be executed. Functions can be passed as arguments to other functions, returned as values from functions, and assigned to variables. You can create a function using the function declaration syntax or the function expression syntax. For example: `// Function declaration function sayHello() { console.log("Hello, World!"); }

// Function expression
let sayGoodbye = function() {
console.log("Goodbye, World!");
};`

Image description

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ

You've missed BigInt in the primitive types

Collapse
 
himanshudevgupta profile image
Himanshu Gupta

ooh yes thankyou