ELOQUENT JAVASCRIPT : CHAPTER 2
Tags: javascript, webdev, codenewbie
Published: 2020-12-11 12:32:36

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





## Table Of Contents
  * [Chapter 1](#chapter-1)
    - [Expressions and statements](#es)
    - [Variables](#v)
    - [Functions](#f)
    - [Control Flow](#cf)
    - [Break, Continue](#bc)
    - [Switch](#s)




*Chapter-2*

####EXPRESSIONS AND STATEMENTS <a name ="es"></a>
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 <a name ="v"></a>
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);
```

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
```
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 <a name ="f"></a>
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

```

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 <a name="cf"></a>

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);

```


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

```


(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
```

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


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


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](https://pbs.twimg.com/media/DuP34l7XgAApQmq.jpg)




(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
}
```


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
```


___
___




####BREAK and CONTINUE <a name="bc"></a>

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
```

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
```


___
___




####SWITCH <a name="s"></a>
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
}
```


___
___




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;
```

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!:smiley:
All feedbacks are welcome :ok_woman:

Connect with me on :
- [Twitter](https://twitter.com/Sakshiii_6)
- [Github](https://github.com/sakshi006)