DEV Community

Cover image for All Data Types in JavaScript
Nirbhay Parmar
Nirbhay Parmar

Posted on • Edited on

All Data Types in JavaScript

If you are a beginner in web development then you must learn the JavaScript. It powers the interactions on each and every webpages. Basically it enables the websites to interact with the user in many ways like music player, alert, pop-ups or a video player or animations.

But to create such interactions on the websites developers need to write the code that take user inputs or call an api to get any data, but to do all this things we need some type of storage container that will store different types of data like numbers, strings, objects or boolean. So each programming languages defines some data types to sort the things out.

The JavaScript is a "dynamically typed" language, we not have to specify the data types at variable declaration unlike C/C++ or Java. Variables can store any data type in it. There are total 8 data types in JavaScript. These are as follows-

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

Number

Numbers as the name suggest, it used to store numbers. Numbers can be integers, fractions, or Infinity/-Infinity and NaN(Not a Number).

Example-

let int = 123; // integer
let fraction = 1.2 // fraction
let infinity = Infinity // Infinity
let notANumber = 0/0 // NaN
Enter fullscreen mode Exit fullscreen mode

String

String is the type of data in which, there is a bunch of alphanumeric characters and other symbols are together. In other words, it is a group of characters. Strings are to be surrounded by quotes, single or double.

Example-

let str = "abc123,./"; 
// string can have alphanumeric and other symbols
alert( `The back-tics can used to use variables in in between the string like str is- ${str}!` ); 
// The back-tics can used to use variables in in between the string like str is- abc123,./ 
Enter fullscreen mode Exit fullscreen mode

BigInt

BigInt is made to accommodate really large numbers which are more than 2^53 -1 or less -(2^53 -1) due to its technical limitations. They are represented by appending 'n' after the number.

Example-

let bigint = 123456789123456789123456789n;
Enter fullscreen mode Exit fullscreen mode

Boolean

Boolean is used to represent true or false values. Sometimes we only two values for our function or we have to check some condition that is true or false.
Boolean values can also come as a result of comparision.

Example-

let isTrueOrFalse = true;
let firstIsGreaterOrNot = 7 > 9;
console.log(firstIsGreaterOrNot); // false
Enter fullscreen mode Exit fullscreen mode

Null

People often confused between null value and undefined value(non existing value). Null values represents "nothing", "empty" or "unknown".

Example-

let value = null;
console.log(value); // null
Enter fullscreen mode Exit fullscreen mode

Undefined

They are different then Null data types. They represents that the variable is not assigned any value.

Example-

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

Object

Objects are non primitive data type. We can defined key-value pairs. We can store any type values in one such object. We can store collections of data of different data types.

Example-

let obj = 
{
 name: Nirbhay,
 age: 19,
 isIndian: true
}
Enter fullscreen mode Exit fullscreen mode

Symbol

Symbol are used to create unique identifiers for objects. They can used to generate unique identifiers.

No example for this because i have to read more on it.

This post is based on what I learned about data types in JavaScript from javascript.info. It is basically a summary of that article. Visit it to get some deeper understanding.

cover photo by Pankaj Patel on Unsplash

Top comments (8)

Collapse
 
abodactyl profile image
Abby Redwood

Nice post but you should include that these are actually primitive types not types in general - classes or custom types with TypeScript also count as types and this can make it misleading to newer developers.

Collapse
 
nirbhayparmar profile image
Nirbhay Parmar

Thank you for your suggestion. But I don't know about TypeScript and this post is intended more for beginners so i kept it only upto JavaScript.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

For an example of what can be done with Symbol - check out my project

Collapse
 
nirbhayparmar profile image
Nirbhay Parmar

Thank you for showing your idea.

Collapse
 
sfleroy profile image
Leroy

No honerable mentions for Function or NaN? :)

Collapse
 
nirbhayparmar profile image
Nirbhay Parmar

Yeah sure I will include it.

Collapse
 
andykras profile image
Andrey Krasnov

switch (ev.type)

Collapse
 
nirbhayparmar profile image
Nirbhay Parmar

What do you mean?