Chapter 2: Program Structure
Tags: teamtanayejschallenge
Published: 2020-12-03 19:34:05

This is the second blog in the series of twenty-one blogs that encapsulate my learnings from the book Eloquent JS. So Chapter one was said to be nouns and fragments of a sentence while this chapter would thread them to be meaningful prose.

Expressions:
In chapter 1 we created new values but they are of use only if they are framed properly. Expression is not grumpy, happy,sad...A fragment of code that produces a value is called an expression.

Expressions can contain other expressions
NOTE: Expression: Sentence Fragment :: JS Statement: Full Statement
A program is a list of statements.

 A statement stands on its own, so it amounts to something only if it affects the world. It could display something on the screen—that counts as changing the world—or it could change the internal state of the machine in a way that will affect the statements that come after it. These changes are called side effects.

In some cases, JavaScript allows you to omit the semicolon at the end of a statement. In other cases, it has to be there, or the next line will be treated as part of the same statement.TO AVOID ALL FORMS OF CONFUSION JUST USE A SEMI-COLON.

Bindings/Variables:
(a)Nomenclature: Binding names can be any word. Digits can be part of binding names, but the name must not start with a digit. A binding name may include dollar signs ($) or underscores (_) but no other punctuation or special characters. Must not be a keyword too!

(b)Using Capitals while naming Variables: Most JavaScript programmers, follow the bottom style—they capitalize every word except the first.

(c) About: To catch and hold values, JavaScript provides a thing called a binding or variable. That’s the second kind of statement. The special word (keyword) 'let' indicates that this sentence is going to define binding. It is followed by the name of the binding
A single 'let' statement may define multiple bindings.
After a binding has been defined, its name can be used as an expression
When a binding points at a value, that does not mean it is tied to that value forever.

You should imagine bindings as tentacles, rather than boxes. They do not contain values; they grasp them—two bindings can refer to the same value. A program can access only the values that it still has a reference to. When you need to remember something, you grow a tentacle to hold on to it or you reattach one of your existing tentacles to it.
When you define a binding without giving it a value, the tentacle has nothing to grasp, so it ends in thin air. If you ask for the value of an empty binding, you’ll get the value undefined.

The Environment:
The collection of bindings and their values that exist at a given time is called the environment. When a program starts up, this environment is not empty.

Functions:
A function is a piece of program wrapped in a value. Such values can be applied in order to run the wrapped program.
Prompt ----> input function.
Console.log ----> output function.
Executing a function is called invoking, calling, or applying it. You can call a function by putting parentheses after an expression.
Values given to functions are called arguments.

FUN FACT: Though binding names cannot contain period characters, console.log does have one. This is because console.log isn’t a simple binding. It is actually an expression that retrieves the log property from the value held by the console binding. 

Return Values:
When a function produces a value, it is said to return that value. Anything that produces a value is an expression in JavaScript

Control Flow:
When your program contains more than one statement, the statements are executed as if they are a story, from top to bottom.....nothing more than this

Indentation:
You could write your entire code in one line and it would still work. But could you go back and read it? No. Hence, Indentation is important.

LOOPS:
(a) If/Else: Conditional execution is created with the if keyword in JavaScript. In the simple case, we want some code to be executed if, and only if, a certain condition holds.

The if keyword executes or skips a statement depending on the value of a Boolean expression. The deciding expression is written after the keyword, between parentheses, followed by the statement to execute.

The braces can be used to group any number of statements into a single statement, called a block.

 You can use the else keyword, together with if, to create two separate, alternative execution paths. You can youse multiple if/else/elseif pairs.

(b)While and Do Loops:
What is a loop? to run a piece of code multiple times. This form of control flow is called a loop. Looping control flow allows us to go back to some point in the program where we were before and repeat it with our current program state.

A statement starting with the keyword while creates a loop. The word while is followed by an expression in parentheses and then a statement, much like if

A do loop is a control structure similar to a while loop. It differs only on one point: a do loop always executes its body at least once, and it starts testing whether it should stop only after that first execution. 

(c)For Loops:First a “counter” binding is created to track the progress of the loop. Then comes a while loop, usually with a test expression that checks whether the counter has reached its end value. At the end of the loop body, the counter is updated to track progress.Basically, Initialisation ---> Checks ---> Updation.

Breaking out of the loop:
Having the looping condition produce false is not the only way a loop can finish. There is a special statement called break(like a code suicide) that has the effect of immediately jumping out of the enclosing loop.
If you were to remove that break statement or you accidentally write an end condition that always produces true, your program would get stuck in an infinite loop. A program stuck in an infinite loop will never finish running, which is usually a bad thing.(ooopsie..)

Comments: Comments in my opinion are like multivitamins that we take every day. You won't die without taking them but taking them keeps you healthy. Similarly, writing comments on your code is not essential but is surely a healthy practice. Helps you to re-collect easily.

//---> single line comments
/* comment here */ ---> multiple line comments

Happy Reading!
SriV