DataTypes and Scope In JavaScript

DataTypes and Scope In JavaScript

JavaScript is the most popular language in the world and it runs on the most popular environment, meaning the web (browser). By using JavaScript, you can create a 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 which 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, and it is treated as string datatype.
  3. Boolean: You can assign true and false value in this.
  4. Undefined: in this, you declare a variable but you don’t assign the value to it; it is undefined.
  5. Null: if a variable can not contain a value other than null it is null.
Datatypes in JavaScript can be defined at run-time.
Suppose I declared a variable.
  1. Var x=10;
And after that, I assign a string value like this x=”sagar” so at run-time, if you use the code like below, you will know the data types are changed. 
  1. var x=10;  
  2. console.log(typeof(x));//number  
  3. x="sagar";  
  4. console.log(typeof(x)); //string  
Points to remember:
  1. Datatypes are not strongly typed like in Java or C# language.
  2. Whatever value you assigned of the 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.
  1. var x=10; // global scope  
  2. console.log(x); //output-10  
  3. function PrintX()  
  4. {  
  5. var x=12; //private scope  
  6. console.log(x); //output-12  
  7. }  
  8. PrintX();   

Global Scope In JavaScript

In this above example, you can see x variable which is declared outside the function.  Printx is treated as a global variable. And this global variable is accessed 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 the below example.
  1. function PrintX()  
  2. {  
  3. var x=12; //private scope  
  4. console.log(x); //output-12  
  5. }  
  6. console.log(x);  
  7. PrintX();  
In this example, if we 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 a lexical scope approach. Lexical approach says that depending on the position of a word the meaning of word changes. In JavaScript depending on the position, left or right, the scope will change 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.
  1. function display() {  
  2. x = 10;  
  3. console.log(x); //output-10  
  4. }  
  5. display();  
  6. console.log(x);////output-10

Use Strict

But to avoid this functionality you can use “use strict”. The use of use strict is that you can’t use a variable before declaration, which means by using this “use strict” we can raise an error if we use a variable before declaration.
  1. "use strict"  
  2. function display() {  
  3. x = 10;  
  4. console.log(x); //output-10  
  5. }  
  6. display();  
  7. console.log(x);////output-10   
The output of this code is as below which throws an error.
  1. index.htm:13 Uncaught ReferenceError: x is not defined
  2. at display (index.htm:13)
  3. at index.htm:17