Devtantra Blog

Tutorials about Android, Angular, ReactJS, PHP, MySQL and Web Development

Variables & Data types

Variables are very important part of any language, because variable store the value which define by user one time and by the help of variable you can call this value and also data types are very much important ,because it tells to compiler/ interpreter/encoder about variable type ; which helps to encoder/compiler to understand the value and its required bit/byte.

variable

In javascript many types of data types are available and these are

TYPE EXAMPLES OF DATA TYPES
Number 1,2,3
String “hifriends”
Boolean true/false
Array [“mks”,”soni”];
null null means value is null. like var x= null;
undefined undefined like var x;
Object This is written in this way “var per = {firstName:”sam”, lastName:”mam”, age:19};”

Checking Datatype
Javascript gives you very cool method , which check the var and then return its type. Let see how

typeof("sam");                 // Returns "string" 
typeof(3.14);                   // Returns "number"
typeof(false);                  // Returns "boolean"
typeof({name:'mom', age:44});  // Returns "object"

 Variables

Variables are use to store data and this is main part of programming . In javascript there are two method to declare the variable and these are following

First method : With the keyword var. For example,var x = 42. This syntax can be used to declare both local and global variables.

Second method : simply assigning it a value. For example,x = 42. This always use for global variable and it cannot be changed at the local level.

var a=123;//number variable
var b;//undefined variable
var c= null;//null variable
console.log("This is numeric variable"+a);//output command
console.log("This is undefined variable"+b);//output command
console.log("This is null variable"+c);//output command

Output

 Global & Local Variables

Global variable are these variable which define in the starting of program and it hold one value like below example

var num = 2;//global
function local() {
var num =1;
 
    console.log("Local "+num);
   
}
local();//output Local1
console.log("Global"+num);//output Global2

Local variables are these variables which define in any function and the scope of these variables are limited.

1
0

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: