javascript variables : Lesson03 Easy To Learn

JavaScript variables

The variable mainly expresses an algebraic value. It holds its value. The variable can also contain texts and arrays in JavaScript. Using variables is very easy and you can modify it at any time in the code stages.




We will present the two most popular formulas for defining JavaScript variables, according to the approved references:




var x = 10;


With the JavaScript interpreter that is fully supported in Internet browsers, the variable is recognized in both ways, but for good inference on the code, it is preferable that the first definition of the variable be in the second way var x = 10. Here it may facilitate the distribution of code operations and knowing where the variable started its way in the job.


Variables contain many advantages that support us along with the code. They indicate a clear and explicit meaning that goes through a journey of modifications and increments and results of operations that are similar to algebra of mathematics, but in fact it is all mathematics:

  • Time saving : Variables generally work to shorten the time. For example, when defining all JavaScript variables in the header of the page and using them later, there is no need to define them again, which reduces the number of estimated lines in our code, and this is the structured code that everyone wants.


  • Variables as illustration : Suppose we have three variables to add two numbers and put their value in the third, so we give an indication of the meaning of the first number, let it be first_num and the second number second_num, and the result is in a third variable named result whose name is clarified for people who want to read the code later to become as follows:
var first_num = 5;
var second_num = 20;
var result = first_num + second_num;


And with the use of operators, we combined these two numbers with each organization inside the result value, so let the goal always be more organization in the code, when we return to the code after a long time we will try to understand it quickly and therefore others understand it specifically if you are a programmer who supports the performance of developers in JavaScript.


  • Assigning : With JavaScript you can assign variables with others , For example, we defined a variable at the beginning and then we need to equate it with another variable without which logic will not work, then we will make the value of the previously defined variable equal to the variable we wanted recently, and this is a simple abstraction method to not tamper with the default values:
var default = 50;
var test = default;


Right definition

In JavaScript variables , There are definitions that are accepted by the compiler such as:

  • result.
  • _result.
  • result20.
  • result_20.
  • result_num_20.


Wrong definition

In JavaScript variables , There are wrong definitions are not allowed in compiler such as: 

  • #result.
  • 1result.
  • result num.
  • result_num 2.
  • _result num.


Variables as string

The use of JavaScript variables is not limited to variables only, they can be defined as texts in this way:

var my_string = "Hello World!";


Variables as a Boolean

Variables of type boolean can also be defined, which are the values ​​that contain either true or false:

var myBoolean = null;
var myBoolean = 1;
var myBoolean = 0;





There is an importance of using variables in programming languages, and they have many advantages when used in JavaScript, namely:

  • They can be defined for later use in different parts of the code.
  • A very short definition syntax that helps you to organize ideas, the use of variables is indispensable in all programming languages.
  • Programmatic clarity during code analysis by pre-defining or using the formula var x = 15.
  • Variables are generally the main clipboard for values ​​that are loaded onto the screen.