What Is Variable In Programming Of Javascript?

In this guide, we will read full article on what is variable in programming for javascript and how many variable are here with the example. These variable reduce the time writting code of hour and give solutions in a short written code. You can facebook sign up with our facebook page to get latest update from us.

Let’s discuss on Javascript Variable!

In JavaScript the var keyword is used to declare the variable e.g. var a; var x; etc.

Example 1:

<!DOCTYPE html>
<html>
<body>
<h2>Variables JavaScript</h2>
<p>In below example, a, b, and z are variables.</p>
<p id="darkweb"></p>
<script>
var a = 97;
var b = 89;
var c = a + b;
document.getElementById("darkweb").innerHTML =
"The value of c: " + c;
</script>
</body>
</html>

Example 2:

<!DOCTYPE html>
<html>
<body>
<h2>Variables JavaScript</h2>
<p id="darkweb"></p>
<script>
var Numb1 = 78;
var Numb2 = 454;
var sum = Numb1 + Numb2;
document.getElementById("darkweb").innerHTML =
"The total: " + sum;
</script>
</body>
</html>

JavaScript Variable Data Type

In JavaScript, the variable can hold the numbers and strings. The string is a text written inside single and double-quotes. If we put any number in quotes then it will be also treated as a string.

Example:

<!DOCTYPE html>
<html>
<body>

<h2>Data Type Variable</h2>
<p>In JavaScript quotes.</p>
<p>In Numbers quotes.</p>
<p id="darkweb"></p>
<script>
var num=1533 ;
var Question = "How are you guys?";
var answer = 'We are fine tom.';
document.getElementById("darkweb").innerHTML =
num + "<br>" + Question + "<br>" + answer;
</script>
</body>
</html>

JavaScript let

In 2015, ECMA-Script2015 introduced two new Keywords of JavaScript. These are let and const.

Before Introducing of ES2015, The JavaScript had only two type of scope i.e. Global and Function Scope.

let and const keywords provide Block scope variables and constant in JavaScript.

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Declaring in JavaScript</h2>
<p id="darkweb"></p>
<script>
var  x = 16;
// Here x is 16
{  
  let x = 55;
  // Here x is 55
}
// Here x is 55
document.getElementById("darkweb").innerHTML = x;
</script>
</body>
</html>

JavaScript Block Scope

If the Variable declared with the var keyword then, they cannot have block scope, and the variable declared inside a block { } then, it can be accessed from outside the Block.

Variables declared with the let keyword can have block scope and if the Variable  declared inside the block {  } then it cannot be accessed from outside the block:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Declaring Using var in JavaScript</h2>
<p id="darkweb"></p>
<script>
var  x = 5;
// Here x is 5
{  
  var x = 20;
  // Here x is 20
}
// Here x is 20
document.getElementById("darkweb").innerHTML = x;
</script>
</body>
</html>

Stay in touch. Ask Questions.
Lean on us for help, strategies and expertise.

Leave a Reply

Your email address will not be published. Required fields are marked *