DEV Community

Cover image for Type Mastery: Understanding and Leveraging JavaScript's Data Types
Md Alfaz Hossain
Md Alfaz Hossain

Posted on

Type Mastery: Understanding and Leveraging JavaScript's Data Types

Hello developers today I will describe in depth of data types in JavaScript. Let's start.....

Every Variable has a data type. Data types specify which kind of data has been stored in a variable. In the border concept, JavaScript has two kinds of data types.

1.Primitive Data
2.Non-primitive Data

Now these data types are subdivided into several data types.

Image description

Primitive Data Types:

Primitive data types are those data types which value once assigned can not changed. There are several primitive data types...

1.Number
2.String
3.Boolean
4.Undefined
5.Null
6.BigInt
7.Symbol

1. Number
The number is a basic data type in JavaScript that can hold decimals as well as values without decimals.

let x=20;
let x=25.52;
Enter fullscreen mode Exit fullscreen mode

2. String
String data types represent textual data that are enclosed in single or double quotes.

let programmingLanguage = "Python"
let name="mr.x"
Enter fullscreen mode Exit fullscreen mode

3. Boolean
Boolean data types represent logical values. It can store two values either true or false. Boolean is often used in conditional statements and comparisons.

let isRaining =true;
or
let isRaining=true
Enter fullscreen mode Exit fullscreen mode

4. Null
JavaScript null means nothing. The types of null is an object. The null data type is the intentional absence of an object value.So if we want a variable to be empty but not undefined set its value to null without a quotation mark.

let name= null
console.log(name)// output is null
Enter fullscreen mode Exit fullscreen mode

5. Undefined
A variable that was declared without value has the value or type undefined.

let name ;
cosnole.log(name) //undefined
Enter fullscreen mode Exit fullscreen mode

6. BigInt
BigInt is a new JavaScript data types which is used to store integer values that are too big.

let x = BigInt("123455777854565468987686546534658787")
Enter fullscreen mode Exit fullscreen mode

7. Symbol
Symbol data types have been used to have a unique result every time they are constructed. You can think of symbols as a special kind of token that can guarantee uniqueness.

Non-primitive or Reference Data:
Non-primitive data types is also known as reference data types because it is not directly store the data value rather than store reference to objects in memory. There are three types of non-primitive data types...

1.Array
2.Object
3.Function

1. Array
JavaScript array is written with square brackets. Array items are separated by a comma. Array data types may contain elements of any data type including number, string, object and even another array. Array indexes are zero-based which means the first item is [0].

let programmingLanguage = ["c","c++","python"]
let oddNumbers =  [201,203,205]

Enter fullscreen mode Exit fullscreen mode

2. Object
The object data type is a versatile and powerful data type in javascript.It allows to store collections of key-value pairs.

let person = {
name: "John Doe",
Age:25,
Profession: "Web Developer"
}
Enter fullscreen mode Exit fullscreen mode

3. Function
The function is also considered a data type in JavaScript.The function is reuseable code that can be called, passed as a argument and returned as values.

function developer(name)
{
console.log("hello"+name);
}
developer("John Doe") //output is "hello John Doe"
Enter fullscreen mode Exit fullscreen mode

Data Types Checking
To check the type of any variable just use typeof operator.

let x=16;
console.log(typeof x) //output is number
let name="John Doe";
console.log(typeof name) //output is string
Enter fullscreen mode Exit fullscreen mode

Data Types Conversion
JavaScript allows both implicit and explicit data type conversion.

Implicit Conversion:
Implicit conversion means JavaScript automatically converts the type of data without instruction from the developer. One example is When adding a number and a string JavaScript will treat them as a string.

let x=16+"python"
console.log(x)//output is 16python
Enter fullscreen mode Exit fullscreen mode

Explicit Conversion:
Explicit Conversion means the developer instructs to convert the value of one data type into another data type using a built-in function.

let numberString = "123"
let number = Number(numberString) //Explicitly convert string to number
cosole.log(number) //output is 123
Enter fullscreen mode Exit fullscreen mode

Top comments (0)