JavaScript Variables JAVASCRIPT

JavaScript Variables  

JavaScript Variables

JavaScript Variables

JavaScript variables are containers for storing data values. And you can define variables with var keyword
Let's take an example and xy, and z, are variables:

Example

var x = 3;
var y = 6;
var z = x + y;

From the above example, you can expect: 

  • x stores the value 3
  • y stores the value 6
  • z stores the value 9

Declaring (Creating) JavaScript Variables

Creating a variable in JavaScript is called "declaring" a variable.

You declare a JavaScript variable with the var keyword:

var carName;

After the declaration, the variable has no value (technically it has the value of undefined).

To assign a value to the variable, use the equal sign:

carName = "Volvo";

You can also assign a value to the variable when you declare it:

var carName = "Volvo";

In the example below, we create a variable called carName and assign the value "Volvo" to it.

Then we "output" the value inside an HTML paragraph with id="demo":

Example

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

<script>
var carName = "Volvo";
document.getElementById("demo").innerHTML = carName;
</script>

Download free E-book of JAVASCRIPT


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries