DEV Community

Cover image for JavaScript Data Types
sumayaakter533
sumayaakter533

Posted on

JavaScript Data Types

JavaScript Data Types

JavaScript provides different data types to hold various kinds of values. There are two main data types in JavaScript.

  1. Primitive type
  2. Non-primitive type

Primitive Data Type

The predefined data types provided by JavaScript, are known as primitive data types. Primitive data types are also known as in-built data types. They can hold a single simple value.

String, Number, BigInt, Boolean, undefined, null, and Symbol are primitive data types in JavaScript.

Number data type

The number type in JavaScript contains both integer and floating-point numbers. Besides these numbers, we also have some special numbers in JavaScript such as Infinity, -Infinity, and NaN (Not-a-Number).

let x = 20;
let y= 15;

console.log(x + y); // Output: 35
console.log(typeof (x + y)); // Output: number
Enter fullscreen mode Exit fullscreen mode

String data type

A string represents textual data. It contains a sequence of characters. For example, "hello", "JavaScript", etc. In JavaScript, strings are surrounded by quotes:

  • Single quotes: 'Hello'
  • Double quotes: "Hello"
  • Backticks: Hello
// string enclosed within single quotes
let language = 'JavaScript';
console.log(language) // Output: JavaScript

// string enclosed within double quotes
let frameWork = "React";
console.log(frameWork); // Output: React

// string enclosed within backticks
let message = `${frameWork} is a ${language} framework`;
console.log(message); // Output: React is a JavaScript framework
Enter fullscreen mode Exit fullscreen mode

Boolean data type

In JavaScript, the Boolean data type represents a logical entity. That has only two values: true or false. Boolean values are usually used in conditional statements like if, else, while, and ternary operators to control the flow of executions based on certain conditions.

  • True: Represents a logical state of being correct or valid.
  • False: Represents a logical state of being incorrect or invalid.
let isAvailable = true;

if (isAvailable) {
    console.log("The item is available.");
} else {
    console.log("The item is not available.");
}

// Output: The item is available.
Enter fullscreen mode Exit fullscreen mode

Undefined data type

In JavaScript, undefined is a special data type and value that indicates a variable has been declared but has not yet been assigned a value. It represents an "uninitialized" or "unknown" state. The type of undefined is undefined.

let x;
console.log(x); // Output: undefined
console.log(typeof x); // Output: "undefined"
Enter fullscreen mode Exit fullscreen mode

Null data type

In JavaScript, null represents no value or nothing. For example,

let text = null;
console.log(text);  // Output: null
Enter fullscreen mode Exit fullscreen mode

Symbol data type

The Symbol data type is a unique and immutable primitive value, introduced in ES6 (ECMAScript 2015). Symbols are primarily used as unique identifiers for object properties, ensuring that no property keys conflict, even if they have the same name.

let symbol1 = Symbol();
let symbol2 = Symbol("description");
let symbol3 = Symbol("description");

console.log(symbol1); // Output: Symbol()
console.log(symbol2); // Output: Symbol(description)
console.log(symbol2 === symbol3); // Output: false (Each symbol is unique)
Enter fullscreen mode Exit fullscreen mode

Non-primitive Data Type

The data types derived from primitive data types of the JavaScript language are known as non-primitive data types. It is also known as derived data types or reference data types. They can hold multiple values. Non-primitive types include Object, Array, and RegExp.

Object data type

In JavaScript, an object is a collection of related data and functions, known as properties and methods. Properties are “key: value” pairs that store data, while methods are functions associated with the object that can manipulate its properties.

let person = {
    name: "John Doe",
    age: 30,
    isEmployed: true,
    greet: function() {
        console.log("Hello, my name is " + this.name);
    }
};

console.log(person.name); // Output: John Doe
person.greet(); // Output: Hello, my name is John Doe
Enter fullscreen mode Exit fullscreen mode

Array data type

In JavaScript, an Array is a special-form object used to store multiple values in a single variable. It can hold various data types and allows for dynamic resizing. Elements are accessed by their index, starting from 0.

// Creating an Array and Initializing with Values
let courses = ['HTML', 'CSS', 'JavaScript', 'React'];

console.log(courses); // [ 'HTML', 'CSS', 'JavaScript', 'React' ]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)