Basic Javascript Guide

Overview


This guides highlights the basic parts of javascript that can be used in davinci blog to add dynamic features or powerful analytics. For programmers with experience programming python, the python to javascript guide may be more useful.

Control Flow

Control flow is a set of language constructs that tell a program which pieces of code to execute. The simplest example of control is the the if statement. consider the following code:


let age = 12;
if(age-10 > 0){
  print("greater than");					
}
else{
  print("less than");
}
            
Try it!

The if statement is structured like the following:



if(expression){
  code to execute if true					
}
else{
  code to execute if false
}
            


The if statement takes an expression, evaluates the expression. If the expression equals true, then the first block of code is executed, otherwise the second block is executed.

For Control Flow

A very important control flow statement is the for statement. It is used to iterate over all the elements of an array. Consider the following

let data = [1,2,3,4,5];

for(let item of data){
  print(item);					
}
            
Try it!

Contents