Sunday, 24 July 2016

JavaScript Variables

Variables
 variables are used to store data temporarily. variable consist of a name, value and memory address.
name is used to identify variable uniquely, value refers to the data stored in the variable, and memory address refers to the memory location of the variable.

declaring a variable
var variablename; //Single variable declared
var variablename1, variablename2, variablename3 // multiple variable declared.

declaring and defining a varible
var variablename=1234;
or
var variablename;
variablename=1234;

JavaScript allows us to store multiple types of values in a variable. Same variable can be used to store both string and a number at a different times in a script. This is why javaScript is considered poorly typed.
variable stores most recent value assigned to it.

example
<!DOCTYPE html>
<html>
<head>
    <title>javaScript Variable</title>
</head>
<body>
    <h1>JavaScript Variable</h1>
    <script>
        var numberLiteral = 23;
        var _floatingpointLiteral = 45.908;
        var $_booleanLiteral = true;
   
        document.write(numberLiteral + "<br />");
        document.write(_floatingpointLiteral + "<br />");
        document.write($_booleanLiteral + "<br />");

        function funcNumber(i) {
            document.write(i + "<br />");
        }
        funcNumber(numberLiteral);
        numberLiteral = "Reusing Number literal";
        funcNumber(numberLiteral);
        numberLiteral = 8.9087;
        funcNumber(numberLiteral);
    </script>
</body>
</html>

0 comments:

Post a Comment