2

All Data Types in JavaScript

 2 years ago
source link: https://dev.to/nirbhayparmar/all-data-types-in-javascript-237a
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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. Undefined
  6. Object
  7. 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
Enter fullscreen modeExit 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 modeExit 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 modeExit 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 modeExit fullscreen mode

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 modeExit 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 modeExit 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 modeExit 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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK