JavaScript Data Types

Back to home
Logicmojo - Updated Aug 28, 2021



Javascript Data Types

In computer languages, data types are used to classify a certain type of data. A number and a string of letters, for example, are two separate forms of data that JavaScript will process differently.

This is significant because the data type you pick will influence the values you can assign to it and the actions you can take with it. To put it another way, understanding the data type of any particular variable is critical to performing operations with variables in JavaScript.

What is a data type?

In programming, a data type is a form of data that is generally expressed in a specific way to distinguish it from other types of data.

Data types available in JavaScript

Number
String
Boolean
Undefined
Null
Symbol (As of EcmaScript 2015)
Object

They can be categorize in : Primitive vs. Non-Primitive

What is a primitive data type

Immutable data types, such as primitive types in JavaScript, are immutable data types that cannot be modified.

These are Number, String, Boolean, Undefined, Null, Symbol

Non-primitive data types:

There is just one non-primitive type, which is the total opposite of a primitive type in that it can be altered. Example is Object data types

String Data Types.

Quotes are used to write strings. You have the option of using single or double quotations. A string (sometimes referred to as a text string) is a collection of characters. Example:

Example: const str = "java"

The preceding example's algorithm is as follows. To begin, we'll make a variable called "str." Then we give the variable the value "java." The "typeof" command is used in the second portion to print the type of our variable, whose name is "str" and whose value is "java."

Number Data Types

There is only one sort of number in JavaScript. It is possible to write numbers with or without decimals.

Example: const n = 2000;

Boolean Data Types.

It can only have two values true or false

Example: const b = true;

Null Data Types

Null means "nothing" in JavaScript. It's intended to be something that doesn't exist in the first place. Unfortunately, the data type null in JavaScript is an object. The fact that typeof null is an object is a defect in JavaScript. It's supposed to be null.

Example: var nulleg = null;

Undefined

If null is an object, an undefined value is assigned to a newly created variable that has no value. It's supposed to be null.

Example: var undefeg


💡 Difference Between Undefined and Null Data Types.

Undefined and null have the same value but are of distinct types.

typeof undefined           // undefined
typeof null                // object
null === undefined         // false
null == undefined          // true



Objects

As name:value pairs, the JavaScript object data type can hold a large number of values. These pairings are important for storing and retrieving data. Name:value pairs separated by colons with curly braces on either side make up the object literal syntax.

A JavaScript object literal looks like this, with whitespaces between attributes, and is typically used to hold data that is related, such as the information included in an ID:

var john = {firstName:"john", lastName:"harris", color:"dark", location:"LA"};



Function Data Types

In JavaScript, functions are one of the most basic building components. In JavaScript, a function is comparable to a procedure — a series of instructions that performs a task or calculates a value — but a process must accept some input and return an output with some evident relationship between the input and the output to qualify as a function. You must define a function someplace in the scope from which you want to call it before you may use it. A function definition (also known as a function declaration or a function statement) begins with the function keyword and continues with the following:

⮞ The name of the function.

⮞ A list of function arguments, separated by commas and enclosed in parenthesis.

⮞ Javascript functions defined in curly braces

function square(number) {
    return number * number;
}
console.log(cubic(3));// Output is: 3*3=9;



Conclusion: At this point, you should have a better understanding of some of the most common data types available in JavaScript. As you work on JavaScript programming projects, each of these data types will become more important.