Skip to main content

Command Palette

Search for a command to run...

Primitive Data Types in JavaScript

Published
3 min readView as Markdown

Introduction

Primitive data types are the lowest level of data types available in any programming language and are not built on any other data types.

JavaScript has basic types such as Number, String, Boolean, null, undefined, BigInt and Symbol. The primary data types are furthermore characterized by the fact that every instance of a kind describes only one value. In contrast, objects or arrays, for example, may accommodate one or more values and properties.


let us dive into each primitive data type

  1. Number

    Number represent ordinary integers and floating numbers in the JavaScript language. It uses the IEEE 754 standard, so all the data is stored as 64 floating-point values. This leads to some limitations of accuracy, especially for large or small numbers.

     // Examples
       let integer = 16;            // Integer value
       let float = 3.14159;         // Floating-point value
       let negative = -26;          // Negative number
       let scientific = 7.5e6;      // Scientific notation, equivalent to 7,500,000
    
  2. String

    String refers to sequences of characters employed for text. Strings are created by wrapping the text with single quotes(), double quotes() or backticks (` ). In string data, all characters are addressed individually, which means we can access all characters individually.

     // Example
     let singleQuotes = 'Hello there';
     let doubleQuotes = "JavaScript is fun!";
     let backticks = `I love coding.`;
    

    Template Literals: String can include expressions within the brackets ${} itself and it can contain or be a variable or even an expression.

     // Example
     let name =  "Isaac";
     let welcome = `hello, ${name}!`; // ${name} indicate Isaac in this example 
     console.log(welcome); // output: hello, isaac
    
  3. Boolean

    Boolean represent logical entities and have only two values: true and false. They are mostly employed in conditions, where you need to decide whether your program should continue or not.

     // Example
     let isLoggedIn = true;
     if (isLoggedIn) {
         console.log("true, User is logged in");
     } else {
         console.log("false, User is not logged in");
     }
     // Output: true, User is logged in
    
  4. Null

    null is an assignment value which means no concrete object value is assigned to that particular variable. It represents zero value, and the number is often used to reset or clear a variable.

     // Example
     let myName = "isaac";
     myName = null;             // Resetting myName to no value
     console.log(myName);       // Output: null
    
  5. Undefined

    The undefined type is a variable that has been declared but the value has not been assigned. Any variable that is not assigned a value at the start time is said to be undefined.

     // Example
     let myName;
     console.log(myName);  // Output: undefined
    
  6. BigInt

    BigInt extends the number type to handle whole numbers more significantly than Number. MAX_SAFE_INTEGER, i.e. (2^53 – 1), and with BigInt, we can perform arithmetic operations that involve these large exact numbers. Values of BigInt are constructed by appending an n to an integer.

     // Example
     let largeNumber = 1234567890n;
     console.log(largeNumber);      // Output:1234567890n
    

    Arithmetic operations can be performed with BigInt values but only of another BigInt not of normal type.

     // Example
     let big1 = 89929292488605n;    // Largest safe integer
     let big2 = 123n;
     let sum = big1 + big2;
     console.log(sum);              // Output: 89929292488728n
    
  7. Symbol

    Symbol is one of the new primitives that is unique and immutable which means it cannot be changed anymore, and is also part of ES6. It is used mainly to generate and store object property codes, thus preventing naming clashes in objects.

// Example
let id = Symbol("key");
let obj = {
    [id]: 123
};
console.log(obj[id]);  // Output: 123

Conclusion

Each of these primitive types has its own function, and JavaScript uses them to control all sorts of data effectively. Together, they enable JavaScript to feature a great number of prevalent types, ranging from simple logical states and unique IDs to huge integers.

Thank you!