JavaScript and You.

Overcoming your Addiction Today

Functions

Functions are the main “building blocks” of the program. They allow the code to be called many times without repetition. This is often referred to as the "gateway" concept.


function showMessage() {
                            alert( 'Hello everyone!' );
                          }
                        

Objects

objects are used to store keyed collections of various data and more complex entities. In JavaScript, objects penetrate almost every aspect of the language. So we must understand them first before going further into the mind of an addict.


let user = {
                            name: "Cody",
                            age: 28,
                            "likes robots": true  
                          };

Arrays

An Array is a tool to store ordered collections. Array elements are numbered, starting with 0. Arrays are identified by the use of [] brackets.


let fruits = [
                            "Apple",
                            "Orange",
                            "Pear",
                          ];

For Loops

The 'for' loop is the most compact form of looping. It includes the following three important parts − The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins. The test statement which will test if a given condition is true or not. If the condition is true, then the code given inside the loop will be executed, otherwise the control will come out of the loop. The iteration statement where you can increase or decrease your counter. You can put all the three parts in a single line separated by semicolons.


for (initialization; test condition; iteration statement){
                                Statement(s) to be executed if test condition is true
                             }

Conditionals

Similar in nature to loops, conditionals determine how execution flows through JavaScript. The traditional argument of whether to use if-else statements or a switch statement applies to JavaScript just as it does to other languages. Since different browsers have implemented different flow control optimizations, it is not always clear which technique to use.


if (found){
                                    //do something
                                } else {
                                    //do something else
                                }
                                
                                switch(found){
                                    case true:
                                        //do something
                                        break;
                                
                                    default:
                                        //do something else
                                }