Skip to main content

What are the Different Data Types in JavaScript

Basically data types are used to classify one particular type of value(data), that can be represented and manipulated data in a programming language.

Introduction of datatype

A datatypes is the specific category of information that a variable contain. It classify types of data such as string, boolean or numbers etc. So that, It determines the possible values for that particular type.

The data-type type also governs the kinds of operations that can be performed on a variable. This is important because the specific data-type you use will determine what values you can assign to it and what you can do to it.

Note : Datatypes in javascript means the type of data stored in variable.

Why data-type is used in programming language ?

The conscept of data-types is often difficult for beginning programmers to grasp because in real life you don't often distinguish among different types of information. If someone asks you for your name, your age or the current time, you dont't usually stop to consider that your name is a text string and that your age and current times are numbers.

  • However, a variables specific data-type is very important in programming because the data-type helps determine how much memory the computer allocates for the data stored in the variable.

  • JavaScript classifies data-type into 3-section :

    1.) Primitive Datatype : Following are the primitive data-type that JavaScript supports.

    • Numbers(Numerical Data).

    • String(Text Data).

    • Boolean.

    • Symbol.

    2.) Special Datatype : Following are the Special data-type that JavaScript supports.

    • Undefined

    • Null

    3.) Non-Primitive Datatype : Following are the non-primitive data-type that JavaScript supports.

    • Array

    • Function, Object and Properties.


1.) Primitive Datatypes :

  • Datatype's that can be assigned only a single value with no additional properties and methods are called primitive types.

  • Primitive defines immutable values and was introduced recently by ECMAScript standard, It can be stored in the fixed amount of memory available.

Let's see an example of primitive data-type :

 ➤ Example : Copying a primitive.

    In this example, The original was not changed, we can only change the copy.

<script>
    var a = 20;  // assign `20` to `a`
    var b = a;   //copy the value of `a` to `b`
    b = 25;      //assign `25` to `b`
    console.log(a)  // =>20
</script>

Note : Primitive types are also known as simple types or scalar types.

JavaScript supports 4-types of primitive data types described in table :

 Datatype Description Example value
Number Represents numeric(Positive or negative numbers with or without decimal places, or number written using exponential notation) values. 9, 9.5 etc.
String A string (or a text string) is a series of characters. "Hello" “Hello Coder” etc.
Boolean A logical value of true or false. true, false.
Symbol (ECMAScript 6)

2.) Special Datatype :

  • Special data-type are also known as Primitive types.

  • Javascript has 2-more literals, null and undefined, that is considered to be objects.

Following are the Special data-type that JavaScript supports, described in table:

 Datatype Description Example value
Undefined represents undefined value(A variable that has never had a value assigned to it, has not been declared, or does not exits). Undefined value
Null represents numeric values(An empty value). Null value

3.) Non-Primitive Datatypes :

  • Datatype's that Objects can contain multiple values are called non-primitive types. It is same as object in the class. We can define attributes and methods to composite data-types.

  • A Non-Primitive datatypes can contain other values. Since the contents of a reference type can't fit in the fixed amount of memory available for a variable, the in memory value of a reference type is the reference itself (a memory address).

  • For instance :

    • Array

    • RegExp

    • Function, Object and Properties.

Let's see an example of non-primitive data-type :

 ➤ Example : Copying a reference;

    The original was also changed, since the reference got copied.

<script>
    var data = {number: 22 };  //assign the reference of a new object to `data`.
    var b = data;   //copy the reference of the object inside `data` to new variable `b`.
    b.number = 75;  //modify the contents of the object `b` refers to.
    console.log(data);  //=> { number : 37 }
</script>

Note : Non-Primitive type are also known as : Reference types or Composite data-type or Complex types or container types.

JavaScript contain following non-primitive data types, described in table:

 Datatype Description
Array represents group of similar values.
RegExp represents regular expression.
Object represents instance through which we can access members.

Comments

Popular Posts

Django static files not working when debug false || debug true

# Django static and media files not working when debug is false In this article you will learn how to fix problem of not loading static files and media in Django even the DEBUG is FALSE. This is the easiest and safest solution. # Problem: Django static and media files not working when debug is false  ➤ Code: settings.py DEBUG = False #True ALLOWED_HOSTS = [ '*' ] #Host name # Problem Fix: Let's see, How you can fix the problem of Django static and media files not working when DEBUB = False : 1.)First way: devserver in insecure mode If you still need to server static locally ( e.g. for testing without debug ) you can run devserver in insecure mode: python manage.py runserver --insecure --insecure: it means you can run serve...

How to remove the date and .html from every blogger post url

#Remove date and .html from blogger post url A Common search term which every blogger search is How to Remove Date From Blogger Post URL or how do I remove date from blogger permalink? Follow the steps below and then date and .html will be removed from the URL of your blogger post. Step 1 : Login to your Blogger blog and select Theme / Template. Step 2 : Click on Edit HTML and paste the below code just above the </head> tag let's see code :   ➤ Code : mycode.js; Copy code <script type='text/javascript' > //<![CDATA[ // BloggerJS v0.3.1 var urlTotal,nextPageToken,postsDatePrefix=!1,accessOnly=!1,useApiV3=!1,apiKey="",blogId="",postsOrPages=["pages","posts"],jsonIndex=1,secondRequest=!0,feedPriority=0,amp="&"[0];function urlVal(){var e=window.location.pathname,t=e.length;return...

How to remove ? m=1 or ?m=0 from blogger post URL

# Remove m=1 From URL of Blogger post A Common search term that every blogger search is How to ?m=1 or ?m=0 from blogger Post URL. We all know that "simplicity is beauty" and you want to clean permalink. So, in this article, I will guide you on how to remove ?m=1 from the blogger URL, and make a simple professional URL. Follow the few steps below and removed ?m=1 from the URL of your blogger post. Step 1 : First, you login into your blogger's dashboard and then select your blog. Step 2 : Click on the Theme option. Step 3 : Click on the customise Step 4 : Click on Edit HTML option. Step 5 : Press (CTRL + F) from the keyboard and type "/head" and then search. ( If you are not understanding, see the below photo ) Step 6 : Now paste the below code just above the </head> tag. let's see code :   ➤ Code : mycode.js; Copy code ...