Data Types in JavaScriptThis is the banner for Data Types in JavaScript

JavaScript Data Types: JavaScript provides different data types to hold different kinds of values. There are two kinds of data types in JavaScript.

  1. Primitive Data Types
  2. Non-Primitive Data Types

JavaScript is a dynamically typed language, which means that you don’t need to specify the variable type as the JavaScript engine uses it dynamically. You must use var here to specify the data type. It can contain any kind of value, such as BooleanNumbers, strings, etc.

Primitive Data Types

  1. String
  2. Number
  3. Boolean
  4. Undefined
  5. Null

1. String:

The String is used to store text. In JavaScript, strings are enclosed in quotes:

  • Single quotes: ‘Hello’
  • Double quotes: “Hello”
  • Back-ticks:  ` Hello `

Example: 

//String examples
const name = 'recursive'  // String using single quotes
const lastName = "minds"  // String using double quotes
const result = `fist name is ${name} and last name is ${lastName}.` // String using backticks

Single quotes and double quotes are pretty much the same and you can use either.

Backticks are generally used when you need to insert variables or expressions into a string. To do this, variables or expressions are enclosed in ${variable or expression} as shown above.

2. Number:

The Number represents integers and floating point numbers (decimal and exponential).

Example:

// Number Data Types Examples
const num1 = 5;  // Integer Value
const num2 = 5.83; // Floating point value
const num2 = 3e5; // 3 * 10^5 exponential value

A number type can also be +Infinity-Infinity, and NaN (not a number).

Example:

const num1 = 5/0;
console.log(num1); // Infinity

const num2 = -7/0;
console.log(num2); // -Infinity

// strings cannot be divided by numbers
const num3 = "abc"/3; 
console.log(num3);  // NaN (Not a Number)

3. Boolean:

This data type represents logical units. Boolean stores one of two values: true or false. It’s easier to think of it as an on/off switch.

Example:

// Example of Boolean data type
const completed = true;
const completed = false;

we will learn more about booleans in comparison and logical operators.

4. Undefined:

The undefined data type represents the value that is not assigned. In JavaScript if a variable is declared but the value is not assigned, then the value of that variable will be undefined.

For example:

// Example of undefined data type
let name;
console.log(name); // undefined

You can also assign undefined a variable as a value.

For example:

// Assigning undefined to variable
let name = undefined;
console.log(name); // undefined

Note: It is recommended not to explicitly assign undefined to a variable. In general, null is used to assign an "unknown" or "empty" value to a variable.

5. Null:

In JavaScript, null is a special value that stores an empty or unknown Value.

Example:

// Example of null data type
const val = null;

The code above suggests that the val the variable is empty.

Non-Primitive Data Types

  1. Object
  2. Array

1. Object:

An object is a non-primitive data type that allows us to store collections of data. Object stores values in key-value pair.

Example:

// Example of object 
const person = {
    firstName: 'Amit',
    lastName: 'Kumar',
    Standard: 12
};

we will learn about objects in detail in upcoming articles.

2. Array:

An array is a special variable that can hold more than one value:

Example:

// Example of array
const arr = ["Suzuki", "Hero", "BMW"];

Thank you so much for reading

Happy Coding

For more detail visit MDN Docs

Some more articles on JavaScript

  1. Introduction to JavaScript
  2. Variable in JavaScript
  3. Let var const in JavaScript

Leave a Reply

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