JavaScript Statements

Profile picture for user arilio666

A statement is something like communication in a sentence between two individuals where they are trying to let the other one know what he is saying in a sentence and what the other person needs to do.

The same concept apply to programming language as well, in a programming language such as javascript, a statement is a set of values, operators, whitespace, semicolon, functions, variables, comments that tell us what the program/code is trying to do/say/execute.

For Example:

var x, y, z;

x = 5 
y = 5;
z = x + y;

console.log(z)
  • Here the first statement tells to initialize the variables which is ending with a semicolon
  • In 2nd and 3rd statement we are assigning value to x and y and asking z to get the sum of x and y.
  • Finally asked to print the variable z for our visual in dom.

So basically a javascript program/javascript statement is often called javascript code.

Semicolons:

A semicolon(;) is normally to separate a javascript statement from the code to make the code more attractive and organized.

For Example:

let x = 10;
let y = 10;
let z = x + y;

Javascript White Space:

  • We want a JS code to be more readable and defined in a way that when someone looks at it, they should clearly understand and readable. So whitespaces are used to make the code more visually organized and readable.
  • Also, a well-experienced developer should always use spaces between the javascript operators (+*%^=).

Without whitespace:

let pName="rob";
let z=x+y;

With whitespace:

let pName = "rob";
let z = x + y;

Javascript line length/breaks:

It is good practice to always avoid a line with which has 80 characters if there is more to come after that it is best practice to break it to the next line. This makes the code more readable.

For Example:

document.findElementById("name").innerHTML = 
"Mr.Robot";

Code Blocks:

A code block is to group a set of lines of code within curly braces {} to execute together as a pack.

For Example:

if()
{
//your set of code goes in this body
}

There are other statements in javascript also such as if, else if, switch, etc These will be discussed in another article in more detail.