Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Primitive Data Types
2.1.
Number
2.1.1.
Example 
2.2.
String
2.2.1.
Example 
2.3.
Boolean
2.3.1.
Example
2.4.
Undefined
2.4.1.
Example
2.5.
Null
2.5.1.
Example 
2.6.
Symbol
2.6.1.
Example 
2.7.
BigInt
2.7.1.
Example 
3.
Non-Primitive Data Types
3.1.
Object
3.1.1.
Example
3.2.
Array
3.2.1.
Example
4.
Frequently Asked Questions
4.1.
Can a JavaScript variable hold more than one data type?
4.2.
How do I check the data type of a variable in JavaScript?
4.3.
What's the difference between null and undefined in JavaScript?
5.
Conclusion
Last Updated: Apr 6, 2024
Easy

Javascript Data Types

Author Gaurav Gandhi
0 upvote

Introduction

JavaScript, is a popular scripting language for web development, works with various kinds of information, known as data types. It's crucial for any web developer to understand this properly as data types lays the foundation of our code or program.

Javascript Data Types

In this article, we'll learn about JavaScript data types, from the basics, like numbers and strings, to more complex structures. 

Primitive Data Types

In JavaScript, the simplest forms of data are known as primitive data types. These are the types that hold a single value.

Number

This type is used for any kind of numerical value, be it an integer (like 5 or -12) or a decimal (which we call a floating-point number, like 3.14). Whether you're counting items or calculating scores, numbers are your go-to in JavaScript.

Example 

let age = 21; // An integer
let price = 19.99; // A floating-point number

String

Strings are sequences of characters, usually representing text. Anything inside quotes is a string, like "Hello, world!" or 'coding is fun'. Strings can include letters, numbers, spaces, and symbols.

Example 

let greeting = 'Hello, world!'; // Using single quotes
let name = "Jane Doe"; // Using double quotes
let message = `Welcome, ${name}!`; // Using backticks for template literals

Boolean

This type has just two possible values: true or false. Booleans are often used in decision-making in code, like checking if a condition is met.

Example

let isStudent = true;
let hasPassed = false;

Undefined

If a variable has been declared but not assigned a value, its type is undefined. It's like a placeholder waiting to be filled.

Example

let result;
console.log(result); // Shows "undefined"

Null

This type represents "no value" or "nothing". It's used to intentionally indicate that a variable is empty or unknown.

Example 

let emptyValue = null;

Symbol

Introduced in the newer versions of JavaScript, symbols are unique and immutable, often used to create private object properties or to add unique property keys to objects.

Example 

let id = Symbol('id');

BigInt

For numbers larger than the Number type can hold, BigInt comes into play. It can represent very large integers, handy for high precision calculations.

Example 

let largeNumber = BigInt(9007199254740991);
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Non-Primitive Data Types

The more complex data types in Javascript  are called non-primitive data types because they can hold collections of values or more complex entities. Here are the main non-primitive data types in JavaScript:

Object

This is the most common non-primitive data type. An object can hold multiple values in the form of properties and methods. Think of it like a container where you store related information. For example, you can have an object representing a book with properties like title, author, and page count.

Example

let person = {
    name: "John Doe",
    age: 30,
    isStudent: false
};


In this example, person is an object with properties name, age, and isStudent.

Array

An array is a special kind of object used for storing lists of values. Unlike objects, arrays keep their items in an ordered list. You might use an array to keep a list of names, scores, or any other set of values that belong together.

Example

let fruits = ["Apple", "Banana", "Cherry"];


In this case, fruits is an array that stores a list of fruit names.

Frequently Asked Questions

Can a JavaScript variable hold more than one data type?

Yes, JavaScript variables are dynamically typed, meaning a single variable can hold different types of data at different times. For example, a variable initially holding a number can later be assigned a string.

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

You can use the typeof operator to check a variable's data type. For example, typeof "Hello" returns string, and typeof 42 returns number.

What's the difference between null and undefined in JavaScript?

undefined means a variable has been declared but not assigned a value. null is an assignment value, used to represent 'no value' or 'nothing' explicitly.

Conclusion

In this article, we have learned about the basics of JavaScript: data types. We started with primitive data types, which include numbers, strings, booleans, undefined, null, symbols, and BigInt. These are the simplest forms of data that JavaScript can handle directly. We then talked about non-primitive data types, focusing on objects and arrays, which allow us to store and manage collections of data in a structured way.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Javascript Events
Next article
JavaScript Applications
Live masterclass