Eloquent JavaScript Review #Chapter1
Tags: javascript, beginners, review, eloquesntjs
Published: 2021-01-20 13:43:01

##EJS Chapter 1 Review

After the introduction chapter down, here is the second part of the [Eloquent JavaScript]( https://eloquentjavascript.net/) series. This blog will be a review of Chapter 1 of the book. 

>TOC:
1. Number representation
2. Special Numbers
3. Unary, Binary and Ternary Operators
4. Arithmetic and Logical Operators 
5. Strings in JavaScript 
6. Null and Undefined
7. Type Coercion
8. Short Circuit Evaluation

Not delving too deep into what bits, bytes and terabytes, I’m just going to say they are the ways information is stored in a computer’s memory, they take up space and when you delete something they are freed from the information they were holding. They are also known as Values. 

##Number representation
* In JavaScript Numbers (integers) is anything from a whole number to decimal points to exponents. JavaScript uses fixed number of bits to represent a single number, 64 of them t be precise. Hence, the representation is limited. With 64 bits we can represent 264 numbers which equal to 18 Quintilian. 
* But within that 64 bits we also have to represent negative numbers and decimal points as well, so the first bit is the sign bit and each decimal point takes a bit, that makes the representation of numbers somewhat close to 9 Quadrillion which is still a pretty big number. 
* JavaScript numbers are always stored as double precision floating point numbers, following the International IEEE 754 standards.

```javascript
typeof(2893770)    // Number
typeof(2.8973)     // Number   (Floating points are not precise values but an approximation)
typeof(2.998e8)   // Number (2.998e8 = 2.998 x 108 = 299,800,000)  
```

##Special Numbers
* There are three special numbers in JavaScript. They are NaN, Infinity and –Infinity.
* NaN stands for Not a Number. Although it is a number, it’s the only value that is not equal to itself.
```javascript
typeof NaN
// Number
console.log(NaN == NaN)
 // false
```
* Infinity is as usual any number that is too big to be stored in the memory and –Infinity too small a number (NaN being an exception for both). Both are an error value indicating these or a division by zero has happened. 
```javascript
10 / 0
// Infinity
10 / -0
//-Infinity
Infinity - Infinity
//NaN
Infinity / Infinity
// NaN
```

##Arithmetic and Logical Operators 
* Arithmetic Operators are what you have been using almost all your life. These are the basic addition (+), subtraction (-), multiplication (*) and division (/). 
* What might be new to you as a newbie program is the modulus (%) operator. It essentially does is return the remainder. For ex- 314 % 100 produces 14 as the result.
* Logical Operators are of 3 types AND, OR, NOT. They are represented as  such ‘&&’ for AND, ‘||’ for OR and ‘!’ for NOT.

###Precedence of Operators
* It is something that fairly easy to remember for arithmetic operators and most easy trick is the BODMAS rule. However where does the other operators fall in?  Well the logical operators take less precedence over arithmetic operators (except Logical NOT). 
* There are other operators such as Bitwise Operators and   Comparison Operators for a detailed table you can refer the [MDN Docs for Operator Precedence]( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence).

##Strings in JavaScript
* Strings can be triggered using back ticks (``), single quotes (‘’) or double quotes (“”). 
* Escape Character or sequence is a concept which is followed by almost all major modern programming languages. When a character followed by a ‘\’ it becomes a escape sequence or character.
* Some popular sequences are:
 * \b: backspace
 * \t: horizontal tab
 * \n: new line
 * \0: null character

* The strings take up a size of:
 * Pre-ES6 Version: 2 bytes per character
 * ES6 (ECMAScript 6) and later Versions: 2 or 5 bytes per character. The additional 3 bytes is because ES6 adds support for Unicode code point escapes.

*The strings that are inside back ticks(``) are called as template literals. It can embed other values and functions inside it which quotes can’t do. ${expression} will compute the result of the input inside it, convert to string and position in its place. 
~~~
console.log(`Half of 200 is ${200/2}`);
//Output: Half of 200 is 100
~~~

##Unary, Binary and Ternary Operators
* Unary Operators are those Operators that only require a single input value. A suitable example would be the ‘typeof’ operator.

```javascript
console.log(typeof 46.2);
// number
console.log(typeof "Eloquent JS");
// string
```

* Binary Operator similarly require two input values For ex- all Arithmetic Operator require two inputs (an exception would be the minus ‘-’, it can act as both unary as a sign assigner and binary), AND and OR Logical Operators also require two inputs.
 
* Ternary Operator (?:) is used to pick one of two values based on a third value. It’s shorthand for conditional statements. It’s the only operator in JavaScript that takes three inputs. Syntax: condition ? exprIfTrue : exprIfFalse. The exprIfTrue will exceute if the condition is True and exprIfFalse will execute if condition is false.
 
```javascript
let age = prompt('Enter your Age :');
let result = (age < 18 ) ? 'Teen' : 'Adult';
console.log(`You are a ${result}`);
```

##Null and Undefined
* Null and Undefined are used denote the absence of a meaningful value.
* Null simply means empty or non-existent value.
* Undefined means a variable has been declared, but the value of that variable has not yet been defined. It is also returned when there has to be a value returned but the value has no meaning to it.

##Type Coercion
* Type Coersion is a way in which one value’s datatype is converted to another datatype either implicitly. Type Conversion however can be both implicit or explicit. 
* JavaScript usually does coercion this silently and you won’t be able to pinpoint which value was actually changed until you get a garbage or unexpected value. It usually happens when we use “==” or “!=”. To avoid this from happening we can use “===” or “!==”.

```javascript
console.log("5" - 1)
//  4 
//Here the string containing 5 is converted to a number 
```

##Short Circuit Evaluation
* The Logical AND and OR handle the values in a peculiar way. This is known as Short Circuit. What it essentially means is that, Logical OR (‘||’) will return value to its left when the value can be converted to true otherwise it will return the right hand side value. 
* Similarly with Logical AND (‘&&’) it will return the value to its left if the value can be converted to false otherwise it will return the value to its right.

```javascript
true || Logic
//true
```

One important part of JavaScript is sometimes its unpredictable. When the types of values differ, JavaScript uses a complicated and confusing set of rules to determine what to do. In most cases, it just tries to convert one of the values to the other value’s type and vice versa to see if it works.

Reference:
[MDN Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript)
[JavaScript info]( https://javascript.info/)

Well this is what I picked up mostly. Let me know what are your thoughts and any feedback is appreciated. Connect with me through my [Twitter](https://twitter.com/neo_kaizerr) and [LinkedIn](bit.ly/33dlIbc) handles.