Skip to main content

Command Palette

Search for a command to run...

JavaScript Data types

Published
5 min readView as Markdown
JavaScript Data types
T

I'm a recent graduate with a degree in Mechanical Engineering but I have a passion for web development. I have hands on in ReactJS and I have experience building responsive, user-friendly web applications using this framework. I gained hands-on experience in web development through several projects . I'm proficient in HTML, CSS, JavaScript, and I have experience working with various front-end technologies such as ReactJS, Redux, and Bootstrap. I'm also familiar with back-end development such as Node.js and Express.js.

In JavaScript, there are 8 data types:

1. String
2. Number
3. Bigint
4. Boolean
5. Undefined
6. Null
7. Symbol
8. Object

Before beginning with data types we have to know about

What is a data type?

The data type defines the type of data that can be stored in a variable function or return value.

JavaScript Types are Dynamic

JavaScript has dynamic types. This means that the same variable can hold different data types:

let x; // now is x undefined
x=10; // now x is number x="name"; // now x is string x=true; // now x is boolean

String

A string is a series of characters like "My name is Tarun".
Strings are written with quotes. You can use single or double quotes

let x="my name is" ; // here x is my name is let y='my name is' ; // here y is my name is

Double and single quotes output is the same. There’s practically no difference between them in JavaScript.

You will learn more about strings later in this tutorial.

Number

a number used to store numeric values both integers and floating-point numbers

let x=12; // without decimals let y=12.45; // with decimals

Besides regular numbers, 3 special numeric values belong to the number data type:

  • Infinity: It represents a mathematical infinity value(∞), which is greater than any other number. It can be generated by dividing a number by zero or by passing a value that exceeds the maximum numeric value that JavaScript can represent.

    alert(1/0) // Infinity alert(Infinity) //Infinity
    alert(1.7976931348623157e+308 * 2) // Infinity

  • -Infinity: It represents a negative infinity value, which is less than any other number. It can be generated by dividing a negative number by zero or by passing a negative value that exceeds the minimum numeric value that JavaScript can represent.
    alert(-1/0) // -Infinity alert(-Infinity) // -Infinity
    alert(-1.7976931348623157e+308 * 2) // -Infinity

  • NaN: It represents a number that is not a legal number. It is a short form for "Not-a-Number". NaN represents a computational error. It is a result of an incorrect or undefined mathematical operation, for instance:
    console.log("name"/2); // NaN console.log(0 / 0); // NaN console.log(NaN === NaN); // false console.log(isNaN("abc")); // true console.log(typeof NaN); // "number"

    In this example, we see that NaN is of the type number, but represents an undefined or unpredictable value resulting from an operation, such as dividing zero by zero or trying to parse a non-numeric string. When we try to compare two NaN values, the result is always false, because NaN is not equal to any other value, including itself. We can use the isNaN() function to check if a value is NaN or not.

BigInt

JavaScript BigInt variables are used to store big integer values that are too big to be represented by a normal JavaScript Number. the “number” type cannot safely represent integer values larger than (2<sup>53</sup>-1) (9007199254740991), or less than -(2<sup>53</sup>-1) for negatives.
let x = 9999999999999999; //10000000000000000

In this example, we can clearly see that there is a precision error, because not all digits fit into the fixed 64-bit storage. So an “approximate” value may be stored.

To overcome this problem BigInt type was recently added to the language to represent integers of arbitrary length.

How to Create a BigInt

A BigInt value is created by appending n to the end of an integer or by using the BigInt() constructor.
let x = 123456789012345678901234567890n; //123456789012345678901234567890n let y = BigInt(123456789012345678901234567890) //123456789012345678901234567890n

A BigInt can not have decimals
let x = 5n; let y = x / 2; // Error: Cannot mix BigInt and other types, use explicit conversion.

You can check MDN BigInt compatibility table to know which versions of a browser are supported.

Boolean

Booleans can only have two values: true or false. here true means "yes" or 1. false means "No" or 0.

let x = 5; let y = 5; let z = 6; alert(x > y) // Returns false alert(x < z) //Returns true
alert(x===y) //Returns true

Undefined

In javascript, when a variable is declared, but not assigned, then its value is undefined.
let age; alert(age); // displays "undefined"

it is possible to explicitly assign undefined to a variable:
let age = 100; // change the value to undefined age = undefined; alert(age); // "undefined"

undefined is a type itself (undefined)
let x; console.log(typeof x); // undefined

Null

Null represents “nothing”, “empty” or “value unknown”. It can be assigned to a variable as a representation of no value. null is a type of object.

let x=null; // null let y=console.log(typeof x); // object

Symbol

It represents a unique identifier. Symbols are immutable and unique, meaning that once a symbol is created, its value cannot be changed, and no two symbols can have the same value.

Symbols are often used as keys in objects to ensure that the property names are unique and do not clash with other property names. When a symbol is used as a key, it is not exposed to the outside world, making it more secure than using a string as a key.

Symbols are created using the Symbol() function, which returns a new unique symbol value each time it is called. For example:
var s1 = Symbol(); var s2 = Symbol(); console.log(s1 === s2); // false

Object

objects are written with curly braces {} .It is used to store the collection of data in key: value pairs separated by commas.

const person = {
name: 'John', age: 30, address: { street: '123 Main St', city: 'Anytown', state: 'CA' },
};
In this example, the person object has four properties: name, age and address The name and age properties have string and number values, respectively. The address property has an object value with three properties of its own: street, city, and state.

To access the value of a property in an object, you can use dot notation or bracket notation. For example:
console.log(person.name); // 'John' console.log(person['age']); // 30 console.log(person.address.city); // 'Anytown'

The typeof Operator

The typeof operator returns the type of a variable

typeof undefined // "undefined" typeof 0 // "number" typeof 10n // "bigint" typeof true // "boolean" typeof "foo" // "string" typeof Symbol("id") // "symbol" typeof Math // "object" (1) typeof null // "object" (2) typeof alert // "function" (3)

##

More from this blog

J

JSpoint

53 posts

Discover the power of JavaScript with our comprehensive website. Explore resources and practical examples to enhance your coding skills. From frontend development to backend scripting.