All You Need to Know About JavaScript Data Types

All You Need to Know About JavaScript Data Types

What are Datatypes?

Datatypes in any programming language represents the type of value that is to store in a variable and it also determine what kind of operations can be performed on that data and how it will store in the memory.

Types of Datatypes in JavaScript

Datatypes in JavaScript can be categorized in two types

  1. Primitive Datatypes

  2. Object Datatypes (user-defined)

Primitive Datatypes

  • Immutable in nature
What does 'Immutable' means?
The word "immutable" maybe familiar if you are from programming field but don't worry if you are not. I am here, for it ::::)))) . See, Immutable means the once a value is assigned to a variable holding an primitive datatypes then it's reference will change not the original string. For example : let a=10 right? the value of a is 10. now if I modify that value then ? say, a+=10 now, what do you think the value will be? yes, it will be 20 but it seems like we have modified the original value of a but in fact, it is not... However, the variable can point to multiple values as in this case the variable will now point to the updated value but that does not mean it changed or altered the original value.
  • They represent single value

  • we can perform operations but that does not change the original value.

  • value stored in stack data structure

  • They are high in performance

  • They are the foundation for data structure

null, integer, bigInt, string, boolean, undefined, symbol are the primitive datatypes in JavaScript.

Types of Primitive

  • number :- includes Integer ,floating-point numbers, infinity and NaN (Not a number) like 10 2.5 0/0 25/0

  • string :- Represent character or sequence of character or text. Anything written in double commas or single commas shall consider as string like '10' "10" "abc"

  • boolean :- Represents a logical value either true or false

  • null :- Represents non-existing value or empty

  • undefined :- Represents an uninitialised or unknown value. Typically shown when a variable is being used that has not been assigned a value.

  • bigInt :- Represents integers with arbitrary precision like 12547854646464646464n

  • Symbol :- Represents a unique and immutable identifier. Symbols are useful for creating unique keys for object properties like Symbol('id')

Knowing the type of variable

we don't need to add the datatype while assigning a variable. But if we need to know the type of variable then we can do so with type of

let a =10

console.log(typeof a)

Object Datatypes

  • Mutable in nature

  • Non-primitive

  • They are complex

  • They can hold multiple values or data structure.

object, Arrays, function, regular expression, Error and date are considered under object datatypes.

Types of Non-primitive datatypes

we will discuss each one in upcoming articles.

Key differences between Primitive and non-primitive datatypes

Primitive DatatypesObject Datatypes
• Immutable• Mutable
• Compared by values• Compared by references
• on passing a primitive variable to a function, only its copy will pass which do not allow to change its original value.• on passing an object variable, its reference will pass which may allow to change the original object.
• Operations done on primitives, create a new value, meaning the original will be unchanged• Operation done on objects, modified the original object.

For now, its sufficient. However the object datatypes are very lengthy and If discussed now, it may overwhelm. So, we will discuss each user-defined datatype in upcoming articles.

If you want more such content then make sure to following me on all available platform

Instagram

LinkedIn

Hashnode


Thank you for reading 🤍

Written By Mohit Soni