Do you want to know what are the different data types in JavaScript? Read on!
In this article, we will show you the different data types that you should know while working with JavaScript.
By that, you’ll learn how to use them effectively in your programs. This article provides a detailed description and examples of each data type.
If you want to improve your understanding of JavaScript data types, let’s get started!
What are the Data types in JavaScript?
Data types in JavaScript refer to the different types of values that can be stored in a variable.
There are eight (8) data types in JavaScript, that specify what kind of data can be stored and manipulated within a program.
These data types are String, Number, BigInt, Boolean, Undefined, Null, Symbol, and Object.
The first six (6) of these data types (String, Number, BigInt, Boolean, Undefined, and Null) are considered primitive data types, meaning they are the lowest level of implementation of a programming language and are not objects.
The last two (2) (Symbol and Object) are composite data types, meaning they can be composed of other data types.
Here is a brief description and examples of the eight data types in JavaScript:
String
A string is a sequence of characters used to represent text. It is created by enclosing characters in single or double quotes.
✔ Single quotes: ‘Itsourcecode’
✔ Double quotes: “Itsourcecode”
✔ Backticks: `Itsourcecode`
For example:
let samplestring = "Hi, Welcome to Itsourcecode!";
You also have the ability to include quotation marks within a string, provided they do not match the quotation marks that enclose the string.
let samplestring = 'Welcome to "Itsourcecode"';
Number
A number is a numerical value, which can be an integer or a floating-point number.
The majority of programming languages encompass a variety of numeric data types:
✔ Whole numbers (integers):
- byte (8-bit)
- short (16-bit)
- int (32-bit)
- int (32-bit)
✔ Real numbers (floating-point):
- float (32-bit)
- double (64-bit)
In JavaScript, numbers are only of a single type: double (64-bit floating point).
For example:
let samplenumber = 3.14; (With decimal)
let samplenumber = 3; (Without decimal)
The number data type can also represent +Infinity, -Infinity, and NaN (not a number).
Example 1:
const samplenumbe1 = 10/0;
console.log(samplenumbe1);
Output:
Infinity
Example 2:
const samplenumbe2 = -10/0;
console.log(samplenumbe2);
Output:
-Infinity
Example 3:
const samplenumbe3 = "xyz"/1;
console.log(samplenumbe3);
Output:
NaN
📌 Note: Strings can’t be divided to any numbers that’s the reason why it will return Nan.
BigInt
A BigInt is an integer with arbitrary precision, meaning it can represent numbers larger than the maximum safe integer in JavaScript (2^53 – 1).
It is created by appending n to the end of an integer literal.
For example:
let sampleBigInt = 1234567890123456789012345678901234567890n;
📌Please be aware that BigInt was introduced in a more recent JavaScript version and is not compatible with several browsers, including Safari.
Boolean
A boolean represents a logical value, either true or false.
For example:
let a = 10;
let b = 10;
let c = 20;
console.log(a == b); // true
console.log(b == c); // false
Output:
true
false
Undefined
A variable that has been declared but has not been assigned a value is undefined.
For example:
let sampleVariable;
console.log(sampleVariable);
Output:
undefined
Null
The value represents the intentional absence of any object value. It is often used to indicate that a variable should have no value.
In a simple understanding, null is a special value that means something is empty or unknown in JavaScript.
For example:
let sampleVariable = null;
Symbol
A symbol is a unique and immutable primitive value that can be used as the key of an object property. It is created using the Symbol() function.
For example:
let value = Symbol('Itsourcecode');
Object
An object is a collection of properties, where each property has a name and a value.
It can be created using object literal syntax or the Object() constructor.
let sampleObject = {key1: "value1", key2: "value2"};
Here’s a complete example:
const employee = {firstName:"Anna", lastName:"Smith", age:18, address:"USA"};
console.log(employee)
Output:
{ firstName: 'Anna', lastName: 'Smith', age: 18, address: 'USA' }
How many data types are there in JavaScript?
As we mentioned earlier, there are eight data types in JavaScript, that’s include the following:
- String
- Number
- BigInt
- Boolean
- Undefined
- Null
- Symbol
- and Object
How to change data type in JavaScript?
In order to change the data type in JavaScript, it can be done either by using a JavaScript function or automatically by JavaScript itself.
For instance, to convert a string to a number, you can use the global method Number(), which converts a variable (or a value) into a number.
Here’s an example:
let samplestring = "3.14";
let myNumber = Number(samplestring);
console.log(myNumber);
Output:
3.14
Similarly, to convert a number to a string, you can use the global method String(), which can convert numbers to strings. It can be used on any type of numbers, literals, variables, or expressions.
Here’s an example:
let sampleNumber = 18;
let sampleString = String(sampleNumber);
console.log(sampleString);
Output:
"18"
How to check data type in JavaScript?
You can use the typeof operator to check the data type of a variable or value.
The typeof operator is a keyword in JavaScript that returns a string indicating the type of the operand’s value.
It can be used to determine the data type of a variable or expression.
The typeof operator returns a string that indicates the type of the operand that hasn’t been evaluated yet.
Here are some examples of using the typeof operator:
typeof "Itsourcecode" // Returns "string"
typeof 3.14 // Returns "number"
typeof NaN // Returns "number"
typeof false // Returns "boolean"
typeof [10, 20, 30, 40, 50] // Returns "object"
typeof {name:'Anna', age:18} // Returns "object"
typeof new Date() // Returns "object"
typeof function () {} // Returns "function"
typeof myCar // Returns "undefined" *
typeof null // Returns "object"
📌Please note that the typeof operator returns “object” for arrays, dates, and null.
Conclusion
Data types in JavaScript refer to the different types of values that can be stored in a variable and it has eight data types: String, Number, BigInt, Boolean, Undefined, Null, Symbol, and Object.
Each data type has its own unique properties and uses, and understanding how to use them effectively is essential for writing efficient and effective JavaScript programs.
This article has provided detailed descriptions and examples of each data type, along with explanations of how to change and check data types in JavaScript.
We are hoping that this article provides you with enough information that help you understand the data types in JavaScript.
If you want to dive into more JavaScript topics, check out the following articles:
Thank you for reading Itsourcecoders 😊.