What are the Data Types in Javascript

In JavaScript, data types explain the various forms of data that you'll be working with and saving in variables. It is critical that you understand each of these data kinds since data might be saved incorrectly, causing problems in your lines of code later on.

In JS, there are six fundamental data types that may be grouped into three categories: primitive (or primary), composite, and special data types. 

Primitive data types include String, Boolean, and Number. Object, Function, and Array are composite (reference) data types. Null and Undefined, on the other hand, are special data types.

Javascript Data Types With Examples

Data TypesDescriptionExample
StringThis denotes Textual Data"Hello, World", 'Hi TechStack'
BooleanThis simply denotes any of the values - "True" or "False""True" and "False"
NumberThis shows an Integer or a decimal4, 4.3, 8.9e-5
Objecta non-primitive data type that can store collections of Datalet student = { };
FunctionIt executes a block of code
Arrayused for storing multiple variables in just one variable
Nullit denotes a null valuelet x = null;
Undefineda data type with uninitialized variableslet x;

Now, let's extensively take a look at each of the Javascript data types and how you can apply them.

Primitive Javascript Data Types

1. The Number Type

This data type is used to express negative or positive integers with or without a decimal point, as well as values expressed in exponential notation.

var w = 20;         // integer
var x = 40.5;       // floating-point number
var y = 3.35e+5;    // exponential notation, same as 4.25e6 or 4250000
var z = 5.35e-5;    // exponential notation, same as 0.00000425

-Infinity, Infinity and NaN are all special values in this data type. Infinity is the mathematical infinity that is greater than any other number. Infinity is obtained by dividing a number that's not zero by zero, as seen below:

alert(15 / 0);  // Output: Infinity
alert(-15 / 0); // Output: -Infinity
alert(15 / -0); // Output: -Infinity

While NaN denotes a unique Not-a-Number value. 

It is caused by an erroneous or undefined mathematical function. For example;

alert("Some text" / 3);       // Output: NaN
alert("Some text" / 3 + 10);  // Output: NaN
alert(Math.sqrt(-1));         // Output: NaN

2. The Boolean Type

This data type can only have two possible values which are true or false. It is commonly used to hold values like as no (false) or yes (true), off (false) or on (true), and so on, as seen below:

var isTyping = true;   // yes, I'm typing
var isFlying = false; // no, I'm not flying

Boolean values can also be produced as a result of program comparisons. The example below compares two variables and returns the result in an alert message box:

var x = 3, y = 7, z = 12;
alert(y > x) // Output: true
alert(y > z) // Output: false

3. The String Type

Text is stored in the string type. Strings are enclosed by quotations in JavaScript:

'Hello' in single quotes
"Hello" in double quotations
'Hello,' in backticks

//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;

Single and double quotations are virtually identical and can be used interchangeably.

Backticks are commonly used to incorporate variables in a string.

Composite Data Types

1. Array Data Type

An array is a kind of object that allows you to store several variables in a single variable. Each element in an array has a numeric location known as its index and can include any data type-numbers, texts, booleans, objects, functions, and other arrays. The array index begins at zero, therefore the first array value is arr[0] and not arr[1].

var colors = ["Blue", "Red", "Grey", "Yellow"];
var cities = ["Tokyo", "Rio", "Denver"];
alert(colors[0]);   // Output: Blue
alert(cities[2]);   // Output: Denver

2. Function Data Type

A function is an object that runs a section (block) of code. Because functions are objects, they may be assigned to variables, as seen in the following example:

var greeting = function(){ 
    return "Hello TechStack!"; 
// Check the type of greeting variable
alert(typeof greeting) // Output: function
alert(greeting());     // Output: Hello TechStack!

3. Object Data Type

An object is a complicated data type that permits us to save data collections.

const student = {
    lastName: 'ram',
    firstName: null,
    class: 10

Special Data Types

Null and Undefined Data Type

Undefined and Null are simply synonyms for "empty." This means they have no value. The distinction is that undefined occurs when no value is specified.