What Are The Types Of Variables In JavaScript

admin_img Posted By Bajarangi soft , Posted On 06-10-2020

JavaScript variables are containers for storing data values All JavaScript variables must be identified with unique names,These unique names are called identifiers,The general rules for constructing names for variables (unique identifiers) are,Names can contain letters, digits, underscores, and dollar signs,Names must begin with a letter Names can also begin with $ and _ (but we will not use it in this tutorial),Names are case sensitive (y and Y are different variables),Reserved words (like JavaScript keywords) cannot be used as names,JavaScript identifiers are case-sensitive.

JavaScript Variables

1.Create a Basic JavaScript Variable code Example-1

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p>In this example, x, y, and z are variables.</p>

<p id="demo"></p>

<script>
    var x = 5;
    var y = 6;
    var z = x + y;
    document.getElementById("demo").innerHTML =
        "The value of z is: " + z;
</script>

</body>
</html>


2.Example-2
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p id="demo"></p>

<script>
    var price1 = 5;
    var price2 = 6;
    var total = price1 + price2;
    document.getElementById("demo").innerHTML =
        "The total is: " + total;
</script>

</body>
</html>
3.Example-3
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p>Strings are written with quotes.</p>
<p>Numbers are written without quotes.</p>

<p id="demo"></p>

<script>
    var pi = 3.14;
    var person = "John Doe";
    var answer = 'Yes I am!';

    document.getElementById("demo").innerHTML =
        pi + "<br>" + person + "<br>" + answer;
</script>

</body>
</html>

4.Example-4
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Variables</h2>

<p>You can declare many variables in one statement.</p>

<p id="demo"></p>

<script>
    var person = "John Doe", carName = "Volvo", price = 200;
    document.getElementById("demo").innerHTML = carName;
</script>

</body>
</html>


5.Example-5
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript $</h2>

<p>The dollar sign is treated as a letter in JavaScript names.</p>

<p id="demo"></p>

<script>
    var $ = 2;
    var $myMoney = 5;
    document.getElementById("demo").innerHTML = $ + $myMoney;
</script>

</body>
</html>

Related Post