Introduction to JavaScript Data Types With Examples

Introduction to JavaScript Data Types With Examples

5 mins read12.8K Views Comment
Updated on Sep 13, 2023 11:53 IST

JavaScript supports several data types including numbers, strings, booleans, arrays, objects, null, and undefined. Numbers represent numeric values, strings represent text, booleans represent true or false values, arrays store multiple values in an ordered list, objects store key-value pairs, null represents the intentional absence of any object value, and undefined represents the absence of a defined value. Let’s understand!

2021_10_MicrosoftTeams-image-6-1.jpg

JavaScript is a powerful programming language used to make web pages interactive. It is a text-based scripting language that includes data types to identify the data stored in a variable during execution. JavaScript is a dynamically typed language that does not require you to define the data type before declaring a variable. In JavaScript, a variable can be assigned any value. In this blog, you will learn about the most common JavaScript data types with the help of examples.

Want to be a certified JavaScript developer? Explore Online JavaScript Courses

Table of Content

What are Data Types in JavaScript?

Data types in JavaScript define the data type that a variable can store. JavaScript includes primitive and non-primitive data types. The primitive data types in JavaScript include string, number, boolean, undefined, null, and symbol. The non-primitive data type includes the object. A variable of primitive data type can contain only a single value.

Recommended online courses

Best-suited JavaScript courses for you

Learn JavaScript with these high-rated online courses

Free
40 hours
Free
5 hours
Free
33 hours
Free
19 hours
Free
42 hours
Free
– / –
Free
1 week
525
68 hours

JavaScript Data Types

Primitive Data Types

These are the lowest level of data value in the JavaScript programming language. They define immutable values – which can’t be changed.

Explore– Popular Programming Courses

String

A string data type is a group of characters or textual content enclosed by single quotes (‘ ‘) or double-quotes (” “), or tick signs (` `).

For Example

 
var str1 = 'Hello, World!'; // Output: Hello, World! <p>var str2 = "His name is ‘John’"; // Output: His name is ‘John’</p>
Copy code

Also Read: What is Coding: Difference Between Coding And Programming

Number

A number data type is a numeric value. It can be an integer (positive or negative), floating-point, or exponential.

For Example

 
var a=47; // positive integer value
var b=629.5; // floating point
var c=-5; // number with negative value
Copy code

The number data type also includes special values such as Infinity, -Infinity, and Nan (not a number).

Infinity – when a positive number is divided by zero, the result is Infinity

-Infinity – when a negative number is divided by zero, the result is  -Infinity

For Example

 
var x = 25;
var y = -25;
var z = 0;
alert(x/z); // returns Infinity
alert(y/z); // returns -Infinity
Copy code

Nan – when we perform any operation between a number and a non-numeric value, the result is Nan.

For Example

 
var x = ‘hello’;
var y = 100;
alert(x*y); // Nan
Copy code

Boolean

Boolean is a logical data type used to compare two variables or check a condition. It has only two values, namely, true and false.

For Example – Comparing two variables

 
var a =10;
var b=9;
a==b // returns false
Copy code

For Example – Checking a condition

 
If(x<y){
alert(x is a smaller number than y);
}
Copy code
An Overview of JavaScript Classes
An Overview of JavaScript Classes
ES6 introduces JavaScript classes. They are templates for creating JavaScript objects. In a class, data is encapsulated with code that can be used to process it. This blog will give...read more
JavaScript Functions
JavaScript Functions
A JavaScript function is a block of reusable code designed to perform a particular task, enhancing code efficiency and organization. Functions are invoked or called to execute their defined operations...read more
JavaScript Array – How to Use Them?
JavaScript Array – How to Use Them?
JavaScript arrays are versatile, ordered collections of elements that can hold items of any data type, including numbers, strings, or objects. They offer a range of methods for traversal, manipulation,...read more

Undefined

An undefined data type means that a variable is declared and not assigned any value. It is the default value of a variable that has not been defined.

For Example

 
var x; // undefined
console.log(x); // returns undefined
Copy code

Null

A null value means no value. It means an empty value or absence of value.

For Example

 
var x = null;
console.log(x); // returns null
Copy code

Symbol

The symbol data type in JavaScript defines a property of an object which is private to the object. The value with the Symbol data type can be referred to as a symbol value. The symbol refers to the ‘key’ of the key-value pair of an object. Every symbol is unique. Two symbols with the same key values are not the same.

For Example

 
let value1 = Symbol("value");
let value2 = Symbol("value");
alert(value1 == value2); // false
Copy code

In the above example, both  value1 and value2 contain the same key values but they are different as they are of the symbol type.

Understanding JavaScript While Loop
Understanding JavaScript While Loop
The JavaScript while loop continuously executes a block of code as long as a specified condition is true, offering a way to repeat operations efficiently until a certain criterion is...read more
JavaScript Object Methods- How to Use Them?
JavaScript Object Methods- How to Use Them?
JavaScript methods are actions that can be executed on objects. This article will discuss JavaScript method objects, types of JavaScript methods, and their features. You will learn about its various...read more
An Overview of JavaScript Async/Await
An Overview of JavaScript Async/Await
Through this blog, you will learn about javascript async/await functionality but before going in depth about it let’s see what javascript is.

Non-Primitive Data Type

Object

An object data type stores multiple values or data collection regarding properties and methods. It contains key-value pairs in its address. The properties of the object are written as key: value pairs. Each pair is separated by commas, enclosed in curly braces {}. The key must be a string, and the value can be of any data type.

For Example

 
var student = {firstName: 'john', class: 7};
Copy code

Read more: Working with JavaScript Objects

JavaScript Data Types – Summary

Below is a summary of the 7 most common data types in JavaScript

Data Types Description Example
String It contains a group of characters ‘hello’, “hello, world!”
Number Includes an integer or a floating-point number, or an exponential value 5, -56, 2567.54
Boolean Contains any of two values – true or false True and false
undefined It denotes that a variable is just declared and is not assigned any value let a;
null Denotes no value let x = null;
Symbol or an exponential value let value1 = Symbol(‘val’);
Object key-value pairs of collection of data let emp = { };

Also Read: Top JavaScript Interview Questions and Answers

Conclusion

In this article, we explained different data types in JavaScript with examples. We hope that this blog equipped you with a better understanding of the topic and prepared you to move on to creating efficient JavaScript programs.

FAQs

What are the different data types in JavaScript?

JavaScript has several data types, including numbers, strings, booleans, arrays, objects, null, and undefined.

How do I check the data type of a variable in JavaScript?

You can use the "typeof" operator followed by the variable name to determine its data type. For example, "typeof myVariable" will return a string indicating the data type.

Can JavaScript variables change their data type?

Yes, JavaScript variables are dynamically typed, which means they can hold different data types at different times. A variable can be assigned a number and later assigned a string or any other data type.

What is the difference between null and undefined in JavaScript?

"null" is a value that represents the intentional absence of any object value, while "undefined" is a value that indicates the absence of a defined value. "null" is assigned by developers, whereas "undefined" is the default value for variables that have been declared but not assigned a value.

About the Author
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski Read Full Bio