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.
- Number
- String
- Boolean
- 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.
- String: in this, you can assign a single character or multiple characters, and it is treated as string datatype.
- Boolean: You can assign true and false value in this.
- Undefined: in this, you declare a variable but you don’t assign the value to it; it is undefined.
- 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.
- 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.
- var x=10;
- console.log(typeof(x));//number
- x="sagar";
- console.log(typeof(x)); //string
- Datatypes are not strongly typed like in Java or C# language.
- 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.
- var x=10; // global scope
- console.log(x); //output-10
- function PrintX()
- {
- var x=12; //private scope
- console.log(x); //output-12
- }
- PrintX();

Hamid KhanPosted Sep 25, 2019, 7:54 AM
Nice explanation...…..
Praneet RanePosted Sep 24, 2019, 3:00 AM
You can read about Temporal Dead Zone (TDZ) here- https://www.c-sharpcorner.com/blogs/es6-concepts-part-1
Praneet RanePosted Sep 24, 2019, 2:52 AM
Nice blog! But I think few scope chaining concepts are missing here. E.g. Dead zone in scope. If I have varible declaration in outerscope as wel as inner scope the value of the variable in inner scope is undefined untill the redeclaration etc.