Datatypes in JavaScript

Datatypes in JavaScript

In this article, we will learn about Datatypes in JavaScript.

In a JavaScript program, we can use different data types. For example,

const x = 5;
const y = “JavaScript”;

In this example

  • ‘5’ is integer data.
  • ‘Hello’ is string data.

JavaScript DataTypes

In JavaScript, there are eight basic data types. They are as follows:

DataTypesDescriptionExample
Stringrepresents textual data‘hello’, “hello world” etc
Numberan integer or a floating-point number5, 5.134, 3e -2 etc.
BigIntan integer with arbitrary precision900719925124740999n, 2n etc.
BooleanAny of two values: true or falsetrue and false
undefineda data type whose variable is not initializedlet x;
nulldenotes a null valuelet ax = null;
Symboldatatype whose instances are unique and immutablelet message = Symbol(‘hello’);
Objectkey-value pairs of collection of datalet passengers = { };

Except for Object, all data types are primitive, whereas Object is non-primitive.

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

JavaScript String

The text is stored in a string. Quotes are used to surround strings in JavaScript:

  • Single quotes: ‘JavaScript’
  • Double quotes: “JavaScript”
  • Backticks: `JavaScript`

For example:

//strings example
const name = ‘Ramesh’;
const name1 = “Suresh”;
const result = `The names are ${name} and ${name1}`;

Single and double quotes are nearly identical and can be used interchangeably.

When you need to include variables or expressions in a string, you usually use backticks. As shown above, this is accomplished by wrapping variables or expressions in $variable or expression.

JavaScript Number

Integer and floating numbers are represented by the number (decimals and exponentials). For example:

const number1 = 5;
const number2 = 5.143;
const number3 = 5e3 // 5 * 10^3

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

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

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

// strings can’t be divided by numbers
const number3 = “xyz”/5;
console.log(number3); // NaN

JavaScript BigInt

The Number type in JavaScript can only represent numbers that are less than (253 – 1) and greater than – (253 – 1). However, if you need to use a larger number, the BigInt data type can be used.

By appending n to the end of an integer, a BigInt number is created. For example:

// BigInt value
const value1 = 900711231242353252314n;

// Adding two big integers
const result1 = value1 + 1n;
console.log(result1); // “n”

const value2 = 486685465416549898498n;

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

Output:

900711231242353252315n
Uncaught TypeError: Cannot mix BigInt and other types

Note: BigInt was introduced in a newer version of JavaScript, and many browsers, including Safari, do not support it.

JavaScript Boolean

Logical entities are represented by this data type. True or false is the value of a Boolean expression. It’s more straightforward to think of it as a yes/no switch. For example:

const dataAnalysed = true;
const valueCounted = false;

JavaScript undefined

The undefined data type represents an unassigned value. If a variable is declared but not assigned a value, the variable’s value will be undefined. For example:

let number;
console.log(number); // undefined

It’s also possible to make a variable’s value undefined explicitly. For instance:

let number = undefined;
console.log(number); // undefined

Note: It is not recommended to assign undefined to a variable explicitly. Null is typically used to assign a variable an ‘unknown’ or ’empty’ value.

JavaScript null

Null is a special value in JavaScript that represents an empty or unknown value. For example:

const number = null;

The code above suggests that the name variable is empty.

Note: that null is not similar to NULL or Null.

JavaScript Symbol

This data type was added to JavaScript in a later version (from ES2015).

The data type of a value A symbol value is another term for a symbol. A symbol is an immutable, one-of-a-kind primitive value. As an example,

// two symbols with the same description
const message1 = Symbol(‘hello’);
const message2 = Symbol(‘hello’);

Despite the fact that both value1 and value2 contain the word “hello,” they are distinct because they are of the Symbol type.

JavaScript Object

A complex data type that allows us to store collections of data is called an object. For example:

const passenger = {
firstName: ‘suresh’,
lastName: null,
coach: D2
};

JavaScript Type

JavaScript is a loosely typed (dynamically typed) language. The data type of variables is automatically determined by JavaScript.

It also means that a variable can start out as one data type and later change to another. As an example,

// value is of undefined type
let value;

// value is of integer type
value = 5;

// value is of string type
value = “JavaScript”;

JavaScript typeof

The typeof operator can be used to determine the type of a variable. As an example,

const name = ‘shyam’;
typeof(name); // returns “string”

const number = 7;
typeof(number); //returns “number”

const valueChecked = false;
typeof(valueChecked); //returns “boolean”

const a = null;
typeof(a); // returns “object”

It’s worth noting that for the null type, typeof returned “object.” Since the first release of JavaScript, this has been a known issue.

That’s all about the Datatypes in JavaScript.

You may like Variables and Constants in JavaScript.

Hope this article will guide you to recognize about the Datatypes in JavaScript that you needed and still if you have any problems or queries regarding this, post them in the comments section and we will be glad to assist you.

This Post Has One Comment

Leave a Reply