DEV Community

Sakshi Kumar
Sakshi Kumar

Posted on • Updated on

ELOQUENT JAVASCRIPT : CHAPTER 2

In this blog I will cover the things I learnt in Chapter 2 of the book - Eloquent JavaScript.

Table Of Contents

Chapter-2

EXPRESSIONS AND STATEMENTS

Expressions are the fragments of code that produces a value. Every value is an expression.
Statements are the complete sentences that make some sense ,both to humans and computers.
A program is a list of statements grouped together to get the desired output.
Therefore,
Expressions->Statements->Programs



VARIABLES

Variables, also known as bindings are a way to store the values we want to apply calculations upon. Like, we humans need a copy and pen to write down a value and then perform some calculations on it, similarly computers have memory to store the numbers and then perform the calculations we want them to. This is done via variables or bindings. So, variables let us store numbers, strings, result, anything.

var myName = "Sakshi";
console.log(myName);
Enter fullscreen mode Exit fullscreen mode

We can declare a binding either using let, var or const keywords.
They all give almost same result apart from the fact that const is used mostly when we do not want the value of out binding to change, i.e. the value remain constant throughout the program.
We can change the values provided to variables using 'var' and 'let' keywords.
var : Variable
const : Constant

var mySaving = 400;
//if I receive 100Rs this month 
mySaving = mySaving+100;
console.log(mySaving);

//result : 500
Enter fullscreen mode Exit fullscreen mode

If we try to modify a const value during the tenure of a program, we will get an error message!

The variable name could be anything according to our convenience. Although it must not start with a number. Also, we will get error message if we try to name our variable similar to the name of some keyword like : let, break, const, etc.



FUNCTIONS

Functions are nothing but the piece of programs wrapped in a value. It is advisable to use functions in out program as, if once declared they can be used multiple times (otherwise we would have to write the entire code again and again).

//add two numbers
function add(a,b)   //function declaration
{  
  var sum = a+b;
  return sum;
}

var ans = add (4,5);  //function call
console.log("The sum is "+ ans);

result : The sum is 9

Enter fullscreen mode Exit fullscreen mode

Function parameters are the names listed in the function's definition(in this example : a,b). Function arguments are the real values passed to the function.
There are some predefined functions like console.log() and some functions that are user defined - add(), in this case.

console.log() : is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. It prints the output in the console of the browser.

return : The return statement stops the execution of a function and returns a value from that function. The add() function returns the value of sum.



CONTROL FLOW

We can have straight-line execution or conditional execution in our programs.
(A)IF ELSE LOOPS:
The conditional execution can be done using IF-ELSE loop.

//straight line
var age = 34;
console.log(age);

//conditional
var age = 34;
if( age<30)
  console.log("My age is " +age);
else
  console.log("I am older than 30. Age : " + age);

Enter fullscreen mode Exit fullscreen mode

Since there can be multiple conditions, we can use IF-ELSEIF-ELSE loops.

if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
...
else
  statementN

Enter fullscreen mode Exit fullscreen mode

(B) WHILE and DO LOOPS :

While loop is used when we want to execute certain statements multiple times.

var age = 10;
while(age < 15)  //this condition checks if age is less than 15. 
                   If true the inner loop executes.
{
  console.log(age);
  age = age + 1;
}

//result : 10 11 12 13 14
Enter fullscreen mode Exit fullscreen mode

The DO-WHILE loop executes at least once for sure !

let yourName;
do {
yourName = prompt("Who are you?");
} while (!yourName);
console.log(yourName);
Enter fullscreen mode Exit fullscreen mode

This program will force you to enter a name. It will ask again and again until it gets something that is not an empty string. This means the loop continues going round until you provide a non-empty name.

Explaination

(C) FOR LOOPS
Sometimes while loops can be confusing, and therefore for loops come to the rescue. They perform the same function of looping through certain statements repeatedly.

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}
Enter fullscreen mode Exit fullscreen mode

Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block. If condition is false, we come out of the loop.
Statement 3 is executed (every time) after the code block has been executed.

for( var i = 0 ; i < 5 ; i++ )
{
  console.log(i);
}


//result : 0 1 2 3 4
Enter fullscreen mode Exit fullscreen mode


BREAK and CONTINUE

The break statement "jumps out" of a loop. It breaks the loop and continues executing the code after the loop (if any).

for (var i = 0; i < 10; i++) {
  if (i === 3) 
    {  
        break;   //breaks out of the for loop
    }
  console.log(i);
}


//result : 0 1 2
Enter fullscreen mode Exit fullscreen mode

The continue statement "jumps over" one iteration in the loop. It breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

for (var i = 0; i < 6; i++) {
  if (i === 3) 
    {  
        continue;   //goes back to the for loop
    }
  console.log(i);
}

//result : 0 1 2 4 5
Enter fullscreen mode Exit fullscreen mode


SWITCH

The switch statement is used to perform different actions based on different conditions.

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}
Enter fullscreen mode Exit fullscreen mode


camelCase:

The standard JavaScript functions, and most JavaScript programmers, follow the camelCase styleโ€”they capitalize every word except the first.

var myName = "Sakshi";
var newAdditionNumber = 23;
Enter fullscreen mode Exit fullscreen mode

IMPORTANCE OF IDENTATION :

-Easier to read
-Easier to understand
-Easier to modify
-Easier to maintain
-Easier to enhance

IMPORTANCE OF COMMENTS :

-When people work together, comments make it easier for others to read and understand your code.
-If we want to see/edit code later, comments help us to memorize the logic that was written while writing that code.



Thankyou for reading!๐Ÿ˜ƒ
All feedbacks are welcome ๐Ÿ™†โ€โ™€๏ธ

Connect with me on :

Top comments (0)