Data and Variables Part 2


In this lesson we learn In Java script -- How can we use cascading function(+) and some about parseInt and parseFloat method .

How can we set multiple values into var ?

var sum = 4 + 2 ,
difference = 4 - 2 ,
multiply = 4 * 2 ,
divide = 4 / 2 ; // we assign here different values into var together.

alert(sum); // 4+2=6
alert(difference); // 4 - 2 = 2
alert(multiply); // 4 * 2 = 8
alert(divide); // 4 / 2 = 2
alert(sum * multiply); // 6 * 8 = 48

Cascading method : Every programing language have a cascading method. Cascading method is use for join two or more elements together. Java script have a built in cascade function who implied by + sign.

Example: Join two string together --

var foo = "hello world" +", " + " I am here for you!";
alert(foo); // hello world, I am here for you!

Example: Cascade 2 integers with a numeric string --

var foo = 2+3+"4" ; //Here 2 or 3 is integer but 4 is a numeric string so value return 2+3=5+("4") = 54
alert(foo); //we add two integer than join the integer string, because string and integer cant merge than answer return 54

one more this type of example--
var foo= 1+2+"3"+4+5; //here we get result 3345 because compiler calculate values from left to right so 1+2=3 than 3+"3"=33 after than complier read all values after string as string so 33+4=334 and 334+4= 3345=>>(final answer)
//alert(foo); // 3345

parseInt and parseFloat : parsInt function is use for convert numeric string into integer value and parseFloat function is use for convert numeric decimal string or float string into integer data type.



Example of parseInt :

var foo = parseInt("33", 10); //Return 33
var foo = parseInt("33.33" ,10); //Return 33
var foo = parseInt("s" ,10); //Return NaN means Not a number

Example of parseFloat :

var foo = parseFloat("3.14", 10);
alert(foo); // 3.14


Wconcert India © 2015   ·   All Rights Reserved

Free Web Hosting