JS comments, syntax, expressions, statements


JavaScript Comments

A JavaScript comment is a powerful way to deliver a message. During the compilation, it is ignored and will not be executed. Comments are used to add information about the code, warnings, or suggestions so that anyone can easily read and interpret the code later. 

In JavaScript, you can write comments – both single-line comments and multi-line comments.

Single-line Comment is represented by a double forward slash    // …

// It is a single-line comment
// It is also a single-line comment

Multi-line Comment
is represented with an asterisk symbol as    /*   …..   */
/* It is a multi-line comment. 
 A JavaScript multi/line comment 
 can also be referred to as a block comment. 
*/


JavaScript Syntax

JavaScript syntax is the set of rules that creates a correctly structured JavaScript program. To create effective and efficient code, developers understand its syntax.

Example: to create a variable, it is declared using the keyword let. If you try to redeclare a value to this variable, you will get an error.
let myName = "Satyo";
let myName = "Broto";  
//SyntaxError: 'myName' has already been declared


JavaScript Expressions

An expression is a code that computes or evaluates to a value.

For example:
5   // This 'arithmetic expression' evaluates to 5
     
5 + 5   // This 'non-assignment expression' evaluates to 10
     
x = 17   // This 'assignment expression' itself evaluates to 17
     
y = 10 + 5   // This 'assignment arithmetic expression' evaluates
   to 15 and assigns it to the variable y
     
"Satyo" + " " + "Broto"   // This 'string expression' evaluates
   to Satyo Broto
   
50 < 55   // This 'logical expression' evaluates the Boolean
   value of true


JavaScript Statements

A statement is a line of code that performs a specific action or task in a programming language. The JavaScript statements are executed one after another in the order they are written. Statements can end with a semicolon ;. In HTML, JavaScript statements are executed by the web browser.

Declaration Statement: used to declare the creation of variables and functions.
let myValue;
This declaration statement instructs the browser to create a let variable named myValue.

Expression Statement:
myValue  =  15 + 20 ; 
         -expression-
-----statement-------
This expression statement evaluates the value 35 to the variable myValue.

Conditional Statement: used to perform different actions based on different conditions.
if (age < 12) {
       alert('You are a child');
     }
This conditional statement shows a message You are a child and waits for the user to press OK if the age is less than 12.

Loop/Iteration Statement: used to repeat a program’s execution easily and quickly. It offers a DRY (Don’t Repeat Yourself) way to execute code repeatedly. It executes a block of code a number of times. It checks a condition. If the condition returns true, the code will run each time with a different value.
for (let i = 0; i < 7; i++) {
   console.log(i);
}
This Loop/Iteration statement evaluates values to log 0 to 6 to the console.


JavaScript Keywords

JavaScript Keywords are reserved words that cannot be used as names for variables. 

Here is a list of some of the keywords that can be used to create JS statements:

For Declaration Statements: 
let keyword – is used to declare block variable
const keyword – declares a block constant
function keyword – used to declare a function
For Conditionals Statements:
if …else – used to make decisions and execute statements conditionally.
switch – used to check different cases.

For Loops/Iteration statements:
for
while
do…while
for…in
for…of
continue - used to start the next iteration of the loop.
break - used to exit the loop.
return keyword – used to return a value from a function.

For Error Handling Statements: used to test, manage, and create custom error messages.
try – used to test a specific code block for errors
catch – that is executed when an error occurs
finally – used to run some code after try or catch’s execution
throw – used to create a custom error message


Some JavaScript statements are described below:


if statement– used to specify a block of code to be executed, if a specified condition is true.
Syntax: 
if (condition) { 
   // This block of code to be executed if the condition is true 
}
Example: 
let myValue = prompt('Enter your no.:'); //15 
   if(myValue > 10){
      console.log(`The number is greater than 10, and it is ${myValue}`); 
}
//The number is greater than 10, and it is 15

else statement – used to specify a block of code to be executed, if the same condition is false.
Syntax:
if (condition) {
    // This block of code is to be executed if the condition is true
} else {
    // This block of code is to be executed if the condition is false
}
Example:
let myValue = prompt('Enter your no:');            //9

if(myValue > 10){
   console.log(`The number is greater than 10, and it is ${myValue}`);
} else {
   console.log(`The number is less than 10, and it is ${myValue}`);
}
//The number is less than 10, and it is 9

Else if statement – used to specify a new condition to test, if the first condition is false.
Syntax:
if (condition1) {
    // this block of code is to be executed if condition1 is true
} else if (condition2) {
    // this block of code to be executed if condition1 is false 
    and condition2 is true
} else {
    // this block of code is to be executed if condition1 is false
    and condition2 is false
}
Example:
let myValue = prompt('Enter your no:');                //3
if(myValue > 10){
    console.log(`The number is greater than 10, and it is ${myValue}`);
} else if (myValue < 5){
    console.log(`The number is less than 5, and it is ${myValue}`);
} else {
    console.log(`The number is between 5 and 10, it is ${myValue}`);
}
//The number is less than 5, and it is 3

switch statement – evaluate many alternative blocks of code to be executed. break keyword to stop the execution inside the switch block. If break is omitted, the next statement will be executed. If no matching cases are found, the program continues to the default keyword (optional). 
Syntax:
switch(myExpression) {
  case x:
    // code block to execute if x matches the myExpression   
    break;
  case y:
    // code block to execute if x does not match the myExpression but y does 
    break;
  default:
    // code block to execute if no value matches the myExpression
  }
Example:
let myValue = prompt('Enter your no:');              //1
let num = Number(myValue);
switch( num ){
   case 1:
      console.log(`Congratulations!, you hit the number ${myValue}`);
      break;
   case 2:
      console.log(`You enter the second number, it is ${myValue}`);
      break;
   default:
      console.log(`You enter the number ${myValue}`);
}
//Congratulations !, you hit the number 1

for statement – executes a block of code a number of times until a specified condition evaluates to false.
Syntax:
for (initialization; condition; increment/decrement) {
    // code block to execute if the condition is true
}
for loop Flowchart: 
for loop Flowchart

Example:
let myText = '';
for (let i = 0; i <= 3; i++){
   myText += `My number is ${i}\n`;    // \n for new line
}
console.log(myText);
// My number is 0
   My number is 1
   My number is 2
   My number is 3

while statement – executes a block of code as long as a specified condition evaluates to true
Syntax:
while (condition) {
    // code block to be executed
}
For example:
let i = 0;
let x = 0;
while (i < 3) {
   console.log(i,x);
   i++;
   x += i;
}
// 0 0
   1 1
   2 3

do...while statement – always executes a block of code once before the condition is checked.
Syntax:
do {
    // code block to be executed
}
while (condition);
For example:
let i = 2;
let x = 2;
do {
     console.log(i,x);    
     i++;
     x += i;
   }
while (i < 1);
// 2 2

For…of statement – iterates a loop over iterable objects such as String, Array, Map, Set, and so on.
Syntax:
for (value of iterableObject) {
    // code block to be executed
}
For example:
for…of can iterate over string
let myString = 'Broto' ;
let myResult = ' ' ;
for (let value of myString) {
    myResult += value +'\n' ;
}
console.log(myResult);
// B
   r
   o
   t
   o

for…of can iterate over array
const myArray = [5, 10, 14, 17];
let sum = 0;
for (let element of myArray) {
    sum += element;
}
console.log(sum);
// 46

for…in statement – iterates through the properties of an object. Each iteration property returns a key which is used to access the value of the key.
Syntax:
for (key in object) {
    // code block to be executed
}
For example:
const myObj = {
    name : 'Broto',
    age : 30,
    isStudent : false
};
let myKey = ' ';
let myValue = ' ';
let myKeyValue = ' ';
for (let key in myObj) {
    myKey += key + ' ';
    myValue += myObj[key] + ' ';
    myKeyValue += key +':'+ myObj[key]+ '\n';
}
console.log(myKey);         // name age isStudent
console.log(myValue);       //  Broto 30 false
console.log(myKeyValue);    // name:Broto age:30 isStudent:false