ELOQUENT JAVASCRIPT : CHAPTER 1
Tags: javascript, codenewbie, webdev, teamtanayejschallenge
Published: 2020-12-08 07:51:56

So, this is in continuation with my previous post on 'The introduction' of the book. In this blog I will cover the things I learnt in Chapter 1 of the book - Eloquent JavaScript.
[#teamtanayejschallenge](https://ejs-challenge.netlify.app/)




## Table Of Contents
  * [Chapter 1](#chapter-1)
    - [Values in JS](#values)
    - [Numbers](#numbers)
    - [Arithmetic Operations](#ao)
    - [Special Numbers in JS](#sn)
    - [Strings](#s)
    - [Unary and Binary Operators](#uo)
    - [Boolean Values](#bv)
    - [Logical Operators](#lo)
    - [Empty values](#ev)
    - [Automatic Type Conversion](#atc)
    - [Short Circuiting](#sc)

___
  
*CHAPTER-1*<a name = "chapter-1"></a> 
This chapter introduces the atomic elements or the building blocks  of JS such as the simple value types and the operations that can be performed on them.

#### 1. Values is JavaScript<a name = "values"></a>  :
To be able to work with large quantities of bits without getting lost, we must separate them into chunks that represent pieces of information, these are called as values. Though all values are made of bits, they play different roles. Values can be numbers, texts or strings, functions etc.

#### 2. Numbers<a name = "numbers"></a>  :
Numbers are nothing but the numerical values. Given 64 binary
digits, you can represent 2^64 different numbers, that's a lot!
We can also store fractional numbers. They are written using a dot. Example : 
9.81

For very big or very small numbers, you may also use scientific notation by adding an e (for exponent), followed by the exponent of the number.

```
2.998e8
That is 2.998 × 10^8 = 299,800,000.
```

#### 3. Arithmetic Operations <a name = "ao"></a> :
Arithmetic Operations include Addition, Subtraction, Multiplication, Division , Modulus and so on. They take two numbers or operands, operate upon them and produce the output.

Multiplication and Division have same preference which is greater than Addition and Subtraction, which again have same preference.

Addition:
```
var a = 14;
var b = 10;
console.log(a+b);
```
result-24

Subtraction:
```
var a = 14;
var b = 10;
console.log(a-b);
```
result-4

Multiplication:
```
var a = 14;
var b = 10;
console.log(a*b);
```
result-140

Division:
```
var a = 14;
var b = 10;
console.log(a/b);
```
result-1

Modulus:
```
var a = 14;
var b = 10;
console.log(a%b);
```
result-4

#### 4. Special Numbers in JS <a name = "sn"></a>:
There are certain special numbers as well. Infinity and -Infinity, which represent +ve and -ve infinite values respectively are two of them. Since the calculations using infinite values are not trustable, these are not used much.

**NaN :** Not a Number. It is a value of the number type. We get this result when we try to calculate 0/0, infinity-infinity or any other numeric operations that yield an undefined value or a meaningless result.

####5. Strings<a name = "s"></a> :
These are used to represent text. They are always written inside quotes be it double or single. Example:
```
"I am a string"
'I am also a string'
"1.3454"
```
Whenever a '\' is found inside a string -- the character after it is special, this is called escaping a character. A quote after a backslash would not end the string, instead it will be a part of the string. Example:
```
var s = "Hey! My name is \"abcde\". How you doin'?"
console.log(s);

result : Hey! My name is "abcde". How you doin'?
```


#### 6.Unary and binary Operators<a name = "uo"></a> :
The operators which work on a single operand are called unary operators. They require only one value to produce some result. Example : typeof()
```
typeof(3.14) //number
typeof("sakshi") //string
```
All the arithmetic operators we have read earlier are few examples of binary operators as they take two values to output a result.

#### 7.Boolean values<a name = "bv"></a> :
It has only two values - true and false.
```
console.log(3 > 2)
// → true
console.log(3 < 2)
// → false
console.log("Itchy" != "Scratchy")
// → true
console.log("Apple" == "Orange")
// → false

```
All the operations in JavaScript are done from left to right, keeping in mind the precedence of operators.


There is only one value in JS which is not equal to itself : NaN. It is supposed to denote non-sensical computations and as such it is not equal to the result of any other non-sensical computation.
```
console.log(NaN==NaN) //false
```

####8. Logical Operators<a name="lo"></a> :
JS supports 3 logical operators : AND(&&), OR(||) and NOT(!).
The logical operators have lowest precedence.
```
console.log(true && false)
// → false
console.log(true && true)
// → true
console.log(false || true)
// → true
console.log(false || false)
// → false
```

There is a ternary operator as well. As the name suggests, it takes three values as input. If the condition is true, it outputs the first value otherwise second value is displayed as output.
```
console.log(true ? 1 : 2);
// → 1
console.log(false ? 1 : 2);
// → 2
```

#### 9.Empty values <a name="ev"></a> :
Apart from all the values we have read till now, there are two empty values as well : Null and undefined. They are themselves values but carry no information.
They can be used interchangeably.

#### 10.Automatic type conversion<a name="atc"></a> :
In the introduction blog, it was said that JS sometimes go out of it's way to accept almost any program you give.
```
console.log(8 * null)
// → 0
console.log("5" - 1)
// → 4
console.log("5" + 1)
// → 51
console.log("five" * 2)
// → NaN
console.log(false == 0)
// → true

```
Whenever a wrong type is provided, JS converts it according to the type it needs using a set of rules which might sometime output some undesirable results. This is known as type coercion.

* When something that doesn't map to a number in an obvious way, it is converted to a number -> NaN. Any operation with Nan, results in NaN itself. So, if we ever get any such outcome, we need to check for the type conversion errors in our program.

The '==' sign checks for equality. It yields true when both are equal except for NaN.
When type of two values differ, JS has different rules which mostly try to convert one value's type to another for further calculations.
For 'null' and 'undefined' values, the '==' sign yields true only if both sides are one of null or undefined.

When you don't want any type conversions, use triple equal to (===) and (!==) tests for precise equality.


####11. Short Circuiting of Logical Operators<a name="sc"></a> :
The logical operators && and || handle values of different types in a peculiar way. They will convert the value on their left side to Boolean type in order to decide what to do, but depending on the operator and the result of that conversion, they will return either the original left-hand value or the right-hand value.

|| -> will return the value to its left when that can
be converted to true and will return the value on its right otherwise.
```
console.log(null || "user")
// → user
console.log("Agnes" || "user")
// → Agnes
```

&& -> When the value to its left is something that converts to false, it returns that value, and otherwise it returns the value on its right.
```
console.log(null || "user")
// → null
console.log("Agnes" || "user")
// → user
```

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