codedamn logo
codedamn

Data Types in JavaScript

  • Vishnupriya's profile image
    Vishnupriya
    Team codedamn
Data Types in JavaScript

If you are fascinated with the development of websites and you are at the beginning of your journey of making them then you should know about JavaScript. It is used to develop websites, websites server (using NodeJS), and create mobile applications. 

It is an object-oriented programming language that is used by large tech companies and multinational enterprises like Netflix, Microsoft, PayPal, Walmart, Google, LinkedIn, etc. For a beginner who is starting to learn JavaScript, you should know the basic ABCs of JavaScript which are the data types of it. 

In JavaScript there are two types of data:-

  • Primitive(Basic) Data types
    • String
    • Number
    • Boolean
    • Null
    • Undefined
  • Non- Primitive Data types
    • Object
    • Arrays

Let’s first talk about the 5 primitive or the basic data types and then the 2 non-primitive datatypes

Primitive Data Types

1. String

The string is a combination of alphabets, integers, and characters. It is used to store words or sentences. In JavaScript, strings are immutable which means once it is declared, they cannot be changed or modified later.

The string is executed as: –

const fruit = “apple”;  //Double quotes

const  fruit = ‘apple’;  //Single quotes

We can write strings in both double and single quotes and if we want to include quotes inside the string it can be written. 

Example:-

const demo1 = 'My Favorite fruit is “apple” '; // For double quotes

const demo2 = "My Favorite fruit is ‘apple’ "; // For single quotes

2. Number

The number data type will allow you to present a numeric value which can be an Integer or Float(Decimal) value.

Function syntax:-

Number(integer or float value)  //this constructor creates number 

Examples:-

const demo1 =Number(‘504’); 

console.log(demo1); //output is the number 504

 

const demo2 =Number(‘50.001’);

console.log(demo2); //output is the number 50.001

 

If we write string, multiple integers, or float values inside the constructor then it will show NaN( ‘Not a Number’the ).

Examples:-

const demo1 = Number(‘codedamn’); 

console.log(demo1); // output will be NaN(Not a Number)

 

const demo2 = Number(‘25 504’’);

console.log(demo2);  // output will be NaN(Not a Number)

3. Boolean

The boolean type of data shows two values  –  True or False. This data type is used to verify certain conditions by getting boolean values.

Function syntax:-

Boolean() //  this constructor creates boolean object

Examples:-

const demo1 =  Boolean(10000>90); 

console.log(demo1); // This will show output ‘True’

 

const demo 2 = Boolean(10000<90);

 console.log(demo2); // This will show output ‘False’

  4. Null

This type represents only one value which is ‘null’ which means no value is represented by it. The null value shows the disappearance of an object. If the function shows it then, it means the object couldn’t be created.

Syntax:-

null

Example:-

const demo = null; 

console.log(demo); // output will be ‘null’

If we put null in the boolean function then it will show it as false.

Example:-

const demo = Boolean(null); 

console.log(demo); // Output will be false

5. Undefined

As the name of data type undefined represents that variable is not assigned a value, not declared, or is undefined. It is used in loops or conditions. 

Syntax:-

Undefined

If a variable is not assigned with any data type it will show undefined.

Example:-

Const demo;

console.log(demo); // Output will be undefined

If we put undefined in boolean function then it will show it as false.

Example:-

const demo = Boolean(undefined); 

console.log(demo);// Output will be false

Non-Primitive Data Types

1. Object

An object is an instance through which we can access or store the data types like strings, numbers, etc.

Syntax:-

Object():

It helps us to assign a value to a variable.

Example:-

const drink = “IceTea”;

console.log(drink); // Here using an object can give a variable a value to store.

An object can contain many values.

Example:-

const clothes = { type:”T-shirt”’, color:”Red” }

2. Array

An array is a data type that helps variables to hold multiple values. It helps to store values in a single code and saves us from writing a simple code multiple times to store values.

 Example:-

const drinks = [‘Ice Tea’, ’Latte’];

Here we can use one of the array methods to see how many values are there in the array

Example:-

const drinks = [‘Ice Tea’, ’Latte’,’ Frappuccino’, ‘Cappuccino’];

console.log(drink.length) // Output will be 4

And we can also call one of the values from the inside of the array.

Example:-

const drinks = [‘Ice Tea’, ’Latte’,’ Frappuccino’, ‘Cappuccino’];

const third = drinks[2] // Here the output will be ‘Frappuccino’ ( index starts from 0 thus 2 inside square brackets refers to the third value )

 

Conclusion

In this article, you got to know about the most used data types of JavaScript which are used by web developers every day. These data types will help you in coding and will save time on writing unnecessary simple codes multiple times.

If you are excited to learn JavaScript after this article and want to know more about JavaScript you can learn it from scratch here. Thank You for reading! If you have any suggestions or queries, do let us know in the comments.

User avatar