in , ,

JavaScript Data Types

JavaScript Data Types
JavaScript Data Types

In this tutorial, you will learn about the different data types available in JavaScript with the help of examples.

JavaScript variables can hold numerous data types: numbers, strings, objects, and more:

As recommended by the name, data types refer to sorts of data that you can use in your program. For instance,

const x = 5;
const y = "Hello";

Here,

  • 5 is an integer data.
  • “Hello” is a string data.

JavaScript Data Types

There are eight fundamental data types in JavaScript. They are:

Data TypesDescriptionExample
Stringrepresents textual data‘hello’, “hello world!” etc
Numberan integer or a floating-point number3, 3.234, 3e-2 etc.
BigIntan integer with arbitrary precision900719925124740999n , 1n etc.
BooleanAny of two values: true or falsetrue and false
undefineda data type whose variable is not initializedlet a;
nulldenotes a null valuelet a = null;
Symboldata type whose instances are unique and immutablelet value = Symbol(‘hello’);
Objectkey-value pairs of collection of datalet student = { };

Here, all data types aside from Object are primitive data types, while Object is non-primitive.

Note: The Object data type (non-primitive sort) can store assortments of data, whereas the primitive data type can just store a single data.


JavaScript String

String is used to store text. In JavaScript, strings are surrounded by quotes:

  • Single quotes: ‘Hello’
  • Double quotes: “Hello”
  • Backticks: Hello

For example,

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

Single quotes and double quotes are basically the equivalents and you can use both of them.

Backticks are generally used when you need to incorporate variables or articulations into a string. This is finished by wrapping variables or articulations with ${variable or expression} as shown above.


JavaScript Number

The number represents integer and floating numbers (decimals and exponentials). For example,

const number1 = 3;
const number2 = 3.433;
const number3 = 3e5 // 3 * 10^5

A number type can also be +Infinity, -Infinity, and NaN (not a number). For example,

const number1 = 3/0;
console.log(number1); // returns Infinity

const number2 = -3/0;
console.log(number2); // returns -Infinity

// strings can't be divided by numbers
const number3 = "abc"/3; 
console.log(number3);  // returns NaN

JavaScript BigInt

In JavaScript, Number sort can just speak to numbers under (253  – 1) and more than – (2531). However, if you need to use a larger number than that, you can use the BigInt data type.

A BigInt number is made by annexing n to the furthest limit of a number. For instance,

// BigInt value
const value1 = 900719925124740998n;

// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // returns "900719925124740999n"

const value2 = 900719925124740998n;

// Error! BitInt and number cannot be added
const result2 = value2 + 1; 
console.log(result2); 

Output

900719925124740999n
Uncaught TypeError: Cannot mix BigInt and other types

Note: BigInt was introduced in the newer version of JavaScript and is not supported by many browsers including Safari. Visit JavaScript BigInt support to learn more.


JavaScript Boolean

This data type represents logical entities. Boolean speaks to one of two qualities: true or false. It is simpler to consider it a yes/no switch. For instance,

const dataChecked = true;
const valueCounted = false;

JavaScript undefined

The unclear data type speaks to value that isn’t assigned. If a variable is announced however the worth isn’t assigned, at that point, the value of that variable will be undefined. For instance,

let name;
console.log(name); // returns undefined

It is also possible to explicitly assign a variable value undefined. For example,

let name = undefined;
console.log(name); // returns undefined

Note: It is prescribed not to expressly appoint vague to a variable. Typically, invalid is used to appoint ‘obscure’ or ‘void’ worth to a variable.


JavaScript null

In JavaScript, null is a special value that represents an empty or unknown value. For example,

const number = null;

The code above suggests that the number variable is empty.

Note: null is not the same as NULL or Null.


JavaScript Symbol

This data type was presented in a more up to date form of JavaScript (from ES2015).

A worth having the data type Symbol can be referred to as a symbol value. the symbol is an immutable primitive worth that is extraordinary. For instance,

// two symbols with the same description

const value1 = Symbol('hello');
const value2 = Symbol('hello');

Though value1 and value2 both contain ‘hello’, they are different as they are of the Symbol type.


JavaScript Object

An object is a complex data type that allows us to store collections of data. For example,

const student = {
    firstName: 'haroon',
    lastName: null,
    class: 10
};

JavaScript Type

JavaScript is a dynamically typed (loosely typed) language. JavaScript consequently decides the variables’ data type for you.

It also implies that a variable can be of one data type and later it can be changed to another data type. For instance,

// data is of undefined type
let data;

// data is of integer type
data = 5;

// data is of string type
data = "JavaScript Programming";

JavaScript typeof

To find the type of a variable, you can use the typeof operator. For example,

const name = 'haroon';
typeof(name); // returns "string"

const number = 4;
typeof(number); //returns "number"

const valueChecked = true;
typeof(valueChecked); //returns "boolean"

const a = null;
typeof(a); // returns "object"

Notice that typeof returned “object” for the null type. This is a known issue in JavaScript since its first release.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

JavaScript Variables and Constants

JavaScript Variables and Constants

JavaScript Operators

JavaScript Operators