Learn JavaScript Statements





Part 1

Javascript statements are used to control program flow.


Any Javascript code that you will develop, will be comprised of many separate statements.


A Javascript statement can set a variable equal to a value.


Javascript Statements define what the script will do and how.


Code Example:

                
                
                # Statement 1
                var a, b, c;  

                # Statement 2
                a = 100;    

                # Statement 3
                b = 50;   

                # Statement 4
                c = a - b; 
                
                
              
                
            
Try yourself >>




The next statement in the example tells the browser to write "Learn to code" inside an HTML element with the id="test":


Code Example:

                
                
                document.getElementById("test").innerHTML = "Learn to code";

                
                
              
                
            
Try yourself >>



tip: Statements are executed top to bottom, the first statement we writed will be executed first





To separate statements we will use Semicolons:


Code Example:

               
               
               // first statement

               var x, y;//semicolons     

               //second statement, Assign the value 10 to x

x = 10;//semicolons   

//third statement  Assign the value 20 to y

y = 20;//semicolons           

               
               
             
               
           



you can write many statements and seperate them with semicolons in the same line.


Code Example:

               
               
               # two statements in one line.
                x = 10; y = 20;         

               
               
             
               
           







Part 2

Learn Line Length and Line Breaks




In most cases It is best practice to avoid lines with more than 80 characters, if you have to break line the best practice is to break the line after operator.


In the next example we will break line after the operator


Code Example:

                
                
                
                document.getElementById("test").innerHTML =
"Break line!";
                
                
              
                
            








Part 3

Learn JavaScript White Space




If you add spaces between Javascript lines, nothing will happn, you can add spaces if It will be easier to read


The next two lines in the example are equivalent:


Code Example:

                
                
                
                  var car =   "Volvo";
var car="Volvo";
                
                
              
                
            








Part 4

Learn Code Blocks




Use code blocks to contain Javascript statements.


For that we will use curly brackets {...}


The reason to use code blocks is to Set number of statements to be executed together


You will notice code blocks in Javascript functions. (next example will be function.)


Code Example:

                
                
                
                function myFunction() {
  document.getElementById("test1").innerHTML = "Learn to code";
  document.getElementById("test2").innerHTML = "Free with FreeCodeWiki";
  }
                
                
              
                
            
Try yourself >>


We created code block inside the curly brackets { . . . }









Part 5

Learn JavaScript Keywords




Keywords are reserved words, You can't use them as identifiers (variables names, function names, and loop labels)


Here is a list of some of the keywords:


Visit our Reserved Words reference to see a full list of JavaScript keywords. JavaScript keywords.


Keyword Description
var Declares a variable
debugger Stops the execution of JavaScript, and starts the debugging function
break Terminates a switch or a loop
do ... while Executes a block of statements, and repeats the block, while a condition is true
for Marks a block of statements to be executed, as long as a condition is true
if ... else Marks a block of statements to be executed, depending on a condition
function Declares a function
return Exits a function
continue Jumps out of a loop and starts at the top
switch Marks a block of statements to be executed, depending on different cases










with us you can learn to code easily and free, our main goal is to allow students to learn Web Development and other online programming courses in one place, learn from your home.





Navigation