TechMentorAI

Data Types

in JavaScript

Published

Data types in JavaScript determine the type of data that you can store and manipulate within a program. JavaScript is a dynamic type language, meaning you don't have to specify what kind of information will be stored in variable. For example, you can initially use a variable to store a number, and later use the same variable to store text.

Imagine Data Types in JavaScript

Primitive Data Types

JavaScript has seven basic data types or primitive types. They are Number, String, Boolean, Null, Undefined, Symbol, and BigInt.

Number

The Number data type is used to represent positive or negative numbers with or without decimal place, or numbers written using exponential notation (e).

var num = 25; // A simple number
var flo = 80.5; // A number with a decimal point
var exp = 4e+2; // 400

String

The String data type is used for textual data. It must be enclosed in single or double quotes.

var str = 'Hello World';

Boolean

The Boolean data type can only hold two values: true or false. It is typically used to store values like yes (true) or no (false), on (true) or off (false).

var isReading = true; // yes, I'm reading
var isSleeping = false; // no, I'm not sleeping

Null

The Null data type has only one value: null. This means no value or no object. It implies no object, or null string, no valid boolean value, no number and no array object.

var emp = null;

Undefined

A variable that has not been assigned a value is of type 'undefined'.

var und;
console.log(und); // undefined

Symbol

Symbol is a new data type introduced in ECMAScript 2015. A value of type Symbol is created by invoking the function Symbol() which dynamically produces an anonymous unique value.

var sym = Symbol('Hello');

BigInt

BigInt is a numeric data type that can represent integers in the arbitrary precision format. This is used when we need to represent integers that are too large (greater than 2^53-1).

var bigInt = 1234567890123456789012345678901234567890n;

Non-Primitive Data Types

In addition to primitive types, JavaScript also supports non-primitive types, namely Object, Array, and Function.

Object

Objects are variables, but objects can contain many values. These values are written as a list of value pairs (name : value).

var person = {firstName:'John', lastName:'Doe'};

Array

Arrays are special kind of objects that can hold more than one value at a time.

var colors = ['Red', 'Green', 'Blue'];

Function

Functions are block of reusable code that perform a specific task. Functions are executed when they are called or invoked.

function greet() {
    return 'Hello World';
}
greet(); // 'Hello World'

No Time to Read? Learn on the Go!

By reading this article, you've invested 1.75 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.

JavaScript is like a magic language that helps to make websites more interactive and fun. For example, it can power games, animations, and surveys directly on a website.

Imagine Data Types in JavaScript

What are data types in JavaScript?

Just like we have different kinds of toys like cars, balls, and dolls, JavaScript has different kinds of data. These types are: numbers (like 1 or 2), strings (like 'abc' or 'hello'), boolean (either true or false), null (means nothing), undefined (means we don’t know), object (like a toy box that can store many things), and symbol (a unique and cannot be changed).

Number data type

This is like counting toys. We can add numbers, subtract them, or divide them. For example 5, -3, 0.12 are numbers.

let count = 5;

String data type

Strings are like words or sentences. They are always written inside quotes. For example 'Hello', 'abc', '123' are strings.

let word = 'Hello';

Boolean data type

Booleans are like yes or no, true or false answers to a question. They are either true or false.

let isRaining = false;
let isSunny = true;

Null data type

Null is when we have an empty box. It means there is nothing there.

Undefined data type

Undefined is when we don't know what is inside the box. It means we haven't given it a value yet.

Object data type

Objects are like toy boxes. They can hold several things at once. Each thing in the box has a name and a value.

Symbol data type

Symbol is something unique and cannot be changed once it is created. Even if two symbols look the same, they are not the same.

No Time to Read? Learn on the Go!

By reading this article, you've invested 1.30 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.

About author

Roman Y.
Senior Software Engineer at Nike

Why did I decide to launch this website? Drawing from my rich background in interviewing candidates for a variety of developer roles, I've observed a common pattern: many applicants share similar deficiencies in their knowledge during technical interviews. Motivated by this insight, I established this website with the aim of assisting developers in securing their ideal job. Through straightforward and concise articles, my goal is to not only deepen your understanding of programming language nuances but also to equip you with the insights needed to deliver the precise answers interviewers expect. Essentially, you'll be providing the correct response. I encourage you to spread the word about this site on social media platforms. Echoing the wisdom of an Armenian saying: "Do good and cast it into the water."

EXCLUSIVELY

Certain articles only distributed to subscribers through email