Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




JAVASCRIPT - VARIABLES


Variables in JavaScript are storage location where the data(Be it a number or a Name) is stored.It is like a container where something is stored.

Example :



Let us JavaScript back to our school days. You are in the Maths class and your Maths teacher had asked you to add two numbers. i.e. She asked you to add the numbers 5 and 6 and tell her the result.

And you have written the numbers 5 and 6 in your notebook, added them and and told the result 11 to your teacher.


Well! Pyton takes the same step to add two numbers. The only difference is, you have used one notebook to write and add the numbers.


But JavaScript uses different locations to store each number. And they are called as Variables.


Let us see how Variables are used in JavaScript for addition of two numbers.


JavaScript Code to add two numbers


Example :



<html>
<body>
<script language = "javascript" type = "text/javascript">

	var x = 5
	var y = 6
	var z = x + y
	document.write("The added value is : ",z)
			
</script>     
</body>
</html> 


Output :



  The added value is : 11

Now, if you look at the above code, x, y and z are Variables.


And below are the steps involved in addition of the numbers.

  1. x is holding the first number 5.

    var x = 5


    The above line says, create an empty location named x. And assign it with the value 5. And x is called as variable.
    ruby


    Just note that the = in the above code is called assignment operator. It actually means the value 5 at the right should be placed in the storage location/variable x.

  2. y is holding the second number 6.

    var y = 6


    Again, the above line says, create an empty location/variable named y. And assign it with the value 6.
    ruby

  3. And z is holding the added value of (5 + 6).

    z = x + y


    Similarly, the above line says, create an empty location/variable named z. And store the added value to it.
    ruby

  4. Finally, we are printing the added value of (5 + 6) on the screen. Which is stored in z.

    document.write("The added value is : ",z)


    The above document.write(...) statement has two sections.

    First section has the text inside double quotes ".

    "The added value is : "


    And the second section does not have double quotes. And is usually a variable.

    z


    And they are separated by a comma ','.

    "The added value is :",z


    The comma ',' in the above statement is a concatenate operator. i.e. It joins the first and the second section to form a meaningful sentence.

    And we get the below output :

    The added value is : 11

So far we have seen, how to store a number (i.e. var x = 5) in a Variable.


Now, let us see how to store a String(i.e. It could be a Name or a City) in a Variable.


Storing a String in a Variable


Let us say, we want to store the String Hello World in a Variable(Say x).


And the process of storing a String is same as storing a number, but with a mild difference.


Below is the JavaScript Code :


Example :



<html>
<body>
<script language = "javascript" type = "text/javascript">

	var x = "Hello World"
	document.write(x)
			
</script>     
</body>
</html>


Output :



  Hello World

So, in the above code, we have initialised the String Hello World to the variable x.


x = "Hello World"

The only thing to note here is, if you are initialising a String (i.e. Hello World), it should be either in Double Quotes ("") or Single Quotes (''). i.e. "Hello World" or Hello World.

java_Collections

The above line,


x = "Hello World"

Can also be written as,


x = 'Hello World'

Dealing with multiple variable and data


JavaScript also gives us the flexibility to assign one value to multiple variables.


Below is the JavaScript Code :


<html>
<body>
<script language = "javascript" type = "text/javascript">

	var x = y = z = "Hello"
	document.write(x)
	document.write(y)
	document.write(z)

</script>
</body>
</html>

Output :



  HelloHelloHello


So, in the above code, we have three variables x, y and z. And we have assigned the string Hello to all the three variables in a single line.


var x = y = z = "Hello"

And what happens is, three variables, x, y and z are created and assigned with the value Hello.

java_Collections

JavaScript Variable Naming Convention


Just like any other language, there is a rule to declare the Variables.


Although, the Variables can be of any names with some restriction.


Below are the rules :

  1. A lower case Variable is different from an upper case Variable.

    i.e. The lower case variable a and upper case variable A are different.

  2. A Variable should never start with a number.

    i.e. The Variable with name 2y is never allowed.

  3. A Variable can contain alphabets (i.e. A to Z or a to z), numbers (i.e. 0 to 9) or an underscore (i.e. _).

So, if you have two numbers and need to assign them to two Variables. You can either declare the variables as x and y starting with var keyword.


var x = 5

And


var y = 7

But the preferred way to name the Variables would be firstNumber and secondNumber.


var firstNumber = 5

And


var secondNumber = 7

But you can never use Variable names with spaces like first number or variables with a '-' (i.e. first-number).