Home JavaScript Tutorial Data types in JavaScript

Data types in JavaScript

by Anup Maurya
36 minutes read

JavaScript is a dynamic programming language that supports various data types. In this tutorial, we will discuss the different data types in JavaScript.

Data Types in JavaScript

JavaScript supports the following data types:

  1. Primitive Data Types: Primitive data types are the basic data types in JavaScript that are not objects.
  2. Non-Primitive Data Types: Non-primitive data types are objects in JavaScript.

Primitive Data Types

  1. Number: This data type represents numeric values. JavaScript treats all numeric values as floating-point numbers.

Example:

let x = 10;
let y = 3.14;
  1. String: This data type represents a sequence of characters enclosed in single or double quotes.

Example:

let firstName = 'John';
let lastName = "Doe";
  1. Boolean: This data type represents a logical value that can be either true or false.

Example:

let isStudent = true;
let isGraduated = false;
  1. Null: This data type represents a null or empty value.

Example:

let myValue = null;
  1. Undefined: This data type represents a variable that has been declared but not assigned a value.

Example:

let myVariable;
  1. Symbol: This data type represents a unique value that is not equal to any other value.

Example:

let mySymbol = Symbol('mySymbol');

Non-Primitive Data Types

  1. Object: This data type represents a collection of related data or functionality.

Example:

let person = {
    firstName: 'John',
    lastName: 'Doe',
    age: 30
};
  1. Array: This data type represents an ordered collection of values.

Example:

let myArray = [1, 2, 3, 4, 5];
  1. Function: This data type represents a reusable block of code that performs a specific task.

Example:

function addNumbers(x, y) {
    return x + y;
}

Typeof Operator

JavaScript provides the typeof operator to determine the data type of a variable.

Example:

let x = 10;
let y = 'Hello';

console.log(typeof x); // Output: number
console.log(typeof y); // Output: string

Conclusion

In this tutorial, we discussed the different data types in JavaScript. Understanding data types is essential when working with JavaScript because it helps you to write efficient and effective code.

related posts

Leave a Comment