Crus4

logo

JavaScript Data Types (with examples)


JavaScript is a dynamically-typed language that supports a variety of data types. Data types in JavaScript define the type and nature of the data that can be stored in a variable or passed to a function. Understanding the different data types in JavaScript is essential for developing robust and efficient code.

JavaScript Data Types

There are several data types in JavaScript, which can be categorized as follows:

  1. Numbers
  2. Strings
  3. Booleans
  4. Undefined
  5. Null
  6. Symbol
  7. Bigint
  8. Object – Object data type can also contain arrays and date.

JavaScript Numbers

JavaScript number data type represents numerical values, including integers and floating-point numbers. Numeric literals can be represented using decimal, binary, octal, or hexadecimal notation. For example, the following are the valid number literals in JavaScript:

let x = 10;  //Number
let y = 3.14;  //Decimal(floating) number
let z = 0b1010; // Binary literal
let w = 0o10; // Octal literal
let q = 0xA; // Hexadecimal literal

JavaScript Strings

JavaScript string data type represents textual data and is enclosed in single or double quotes. Strings can contain letters, numbers, symbols, and white spaces. String literals can be concatenated using the + operator or the template literals using backticks. For example:

let str1 = "Hello";
let str2 = "World";
let str3 = str1 + str2;

JavaScript Booleans

JavaScript boolean data type represents a logical value of either true or false. Boolean values are often used in conditional statements and logical operations. For example:

let x = 5;
let y = 10;
let z = 12;
(x == y) // returns false
(x < y)  // returns true

JavaScript Undefined

JavaScript undefined data type represents an unintentional non-value. It is assigned to a variable that has not been initialized or to an object property that does not exist. For example:

let x;
console.log(x); // Outputs undefined

JavaScript Null

The null data type represents a deliberate non-value. It is often used to indicate that a variable has no value or that an object property does not exist. For example:

let x = null;

JavaScript Bigint

JavaScript Bigint is used to store integer numbers that are too big to be represented by a normal JavaScript Number. For example:

let x = BigInt("1234567890120987563478537953156974678975669964212456899")

JavaScript Symbols

JavaScript symbol data type was introduced in ES6 and represents a unique identifier. Symbols are often used as property keys in objects to prevent name collisions. For example:

let sym1 = Symbol();
let sym2 = Symbol("key");

JavaScript Objects

JavaScript object data type is a complex data type that can hold properties and methods. Objects are collections of key-value pairs, where the key is a string and the value can be of any data type. Objects are created using the curly braces {} and properties are accessed using the dot notation or the bracket notation. For example:

<script>
let person = {
    name: "John",
    age: 30,
    isMarried: true,
    hobbies: ["reading", "traveling", "hiking"],
    address: {
        street: "123 Main St",
        city: "New York",
        state: "NY",
        zip: "10001"
    },
    sayHello: function() {
        console.log(`Hello, my name is ${this.name}.`);
    }
};

document.write(person.name); // Output: John
document.write(person["age"]); // Output: 30
person.sayHello(); // Output: Hello, my name is John.

JavaScript Arrays

JavaScript arrays are created using the square braces [ ] and inside it each item is separated by commas. Array indexes are started from zero, which means the first item in array is [0], second is [1] and so on. For example:

let a = ["John", "David", "Abdul"]
document.write(a.[1]);

Share This Post!

JavaScript Data Types (with examples)

2 thoughts on “JavaScript Data Types (with examples)

Leave a Reply

Your email address will not be published. Required fields are marked *