Learn JavaScript Variables





Part 1


We use Javascript variables to store data values


In the next example we will declare three Javascript variables


Code Example:

                   
                   y = 5
x = 10
d = y + x
                 
                 
Try yourself >>



explanation:

  • y stores the value 5
  • x stores the value 10
  • d stores the value 15 ( y + x )

Let's summarize: Javascript variables hold values, making it easier for us to calculate data



tip: Try to use JavaScript variables, it is best practice and will save you time when coding, learn to code Efficiently





Part 2

Javascript Assignment Operator



Unlike algebra In Javascript, the sign (=) is used to "assign"


Code Example:

                   
                   x = 1
                   z = 2
                   y = 10
                   y = x + z  
                   #comment y equal to 3 and not 10 + 2 + 1, because we assigned x and z to y
                 
                 

y equal to 3 and not 10 + 2 + 1, because we assigned x and z to y


Try yourself >>


tip: Unlike assignment sign (=), "equal" sign (==) act diffrently.






Part 3

Javascript Equality Operator



In Javascript, the sign (==) is used In a different way than you would expect.


Code Example:

                   
                   x = 99
                   z = 100
                   x == z 
                 
                 

We declared the variable x to 99, and z to 100, but at the end of the example x will be 100, beacuse we used the Equality Operator, don't forget.






Part 4

Learn JavaScript Data Types



Variables can hold numbers or text values.

In javascript we call text variables as strings.

For string we will use 'Mark' (single quotes), for numbers 10 (no quotes).

Code Example:

                   
                   x = 10;
                   z = 'Mark';
                   y = "Jake";
                 
                 




Part 5

Learn JavaScript Identifiers



Each Javascript variable must be unique.

Identifiers (variable name) can be letters, numbers, underscrores, or dollar sign.

Variables are case sensitive (x and X are not the same)

Variable must begin with a letter (unless it is number)

Code Example:

                   
                   x = 'Jake' #good
                   z = '2jake' #error
                   r = 50 #good
                   y = 'code' 
                   d = 'Code' # y and d are not the same variable!
                 
                 




Part 6

Learn JavaScript Arithmetic



You can use arithmetic with javascript, operators such + or = can be used for that.

Code Example:

                   
                   var y = 10 + 10 + 10;
                 
                 
Try yourself >>


You can also use strings:

Code Example:

                   
                   var z = "Jake" + " " + "Mark";
                 
                 
Try yourself >>





Part 7

Learn JavaScript declaration



Declaring a variable is basic command in Javascript

Use var keyword to declare a JavaScript variable

Code Example:

                   
                   var Jake;
                 
                 


Once you declared Jake, you need to add value to it

Next step is to assign a value to Jake

Code Example:

                   
                   Jake = "Boy";
                 
                 


More efficient way is to assign value and declare variable at the same time!

Code Example:

                   
                   var jake = "Boy";
                 
                 


Even more efficient method is to declare and assign many variables at once.

Code Example:

                   
                   var jake = "boy", color = "red", number = 10;
                 
                 



tip: In certain situations developer will declare variable with no value, for example if the value has to be calculated.












Learn to code with FreeCodeWiki, our main goal is to allow students to learn Javascript and other online programming courses free from home.

Navigation