DataTypes and Scope In JavaScript

DataTypes and Scope In JavaScript By Sagar Jaybhay

JavaScript is the most popular language In the world and it runs on the most popular environment means web(browser); By using JavaScript you can create complete web and desktop application.

DataTypes in Javascript:

In JavaScript, there are 3 types of primitive data-types.

  1. Number
  2. String
  3. Boolean
  1. Number: in this, you can assign values like 10,10.2 means even if you assign a floating-point number to a variable it is treated like a number data-types.
  2. String: in this, you can assign a single character or multiple characters it is treated as string datatype.
  3. Boolean: You can assign true and false value in this.
  4. Undefined: in this, you declared a variable but you don’t assign the value to it is undefined.
  5. Null: if a variable can not contain value other than null it is null.

Datatypes in JavaScript can be defined at run-time.

Suppose I declared a variable

Var x=10;

And after that, I assign string value like this x=”sagar” so at run-time if you use like below you will know the data types are changed.

var x=10;
console.log(typeof(x));//number

x="sagar";
console.log(typeof(x)); //string

Points to remember:

  1. Datatypes are not strongly typed like java, c# language.
  2. Whatever value you assigned of right-hand side the left-hand side variable becomes of that data-type.

Scope In JavaScript:

In JavaScript, we have only two scopes Private and Global. In strongly typed language we have a different type of scopes like public, private, protected,internal like that but in JavaScript we have only 2 scopes Private and Global.

var x=10; // global scope
console.log(x); //output-10

function PrintX()
{    
    var x=12; //private scope
    console.log(x); //output-12
}

PrintX();

Global Scope In JavaScript

In this above example, you can see x variable which declared outside the function Printx is treated as a global variable. And this global variable access throughout the page or that respective javascript file.

Private: In this, the variable which is declared inside the function is treated as a private variable and it is not accessed outside that function see below example

function PrintX()
{    
    var x=12; //private scope
    console.log(x); //output-12
}
console.log(x);
PrintX();

In this example, if run this code it throws an error.

ReferenceError: x is not defined

which is a reference error.

To decide the scope of variable in JavaScript, it uses lexical scope approach. So what is meaning lexical approach it says that depending on the position of a word the meaning of word change. In JavaScript depending on the position left or right the scope will change means whether it is inside the function or outside of a function.

Auto global variable or scope:

In JavaScript when you didn’t declare the variable and assign value to that variable it will become a global variable.

function display() {
        x = 10;
        console.log(x);  //output-10
    }

    display();

    console.log(x);////output-10

Use Strict:

But to avoid this functionality you can use “use strict”. So what is the use of use strict is that you can’t use a variable before declaration means by using this    “use strict” we can raise an error if we use a variable before declaration.

"use strict"
    function display() {
        x = 10;
        console.log(x);  //output-10
    }

    display();

    console.log(x);////output-10

the out of this code is as below which throws an error.

index.htm:13 Uncaught ReferenceError: x is not defined
    at display (index.htm:13)
    at index.htm:17

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *