DEV Community

Sakshi Kumar
Sakshi Kumar

Posted on • Edited on

ELOQUENT JAVASCRIPT : CHAPTER 1

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

Table Of Contents


CHAPTER-1
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 :

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 :

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.
Enter fullscreen mode Exit fullscreen mode

3. Arithmetic Operations :

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);
Enter fullscreen mode Exit fullscreen mode

result-24

Subtraction:

var a = 14;
var b = 10;
console.log(a-b);
Enter fullscreen mode Exit fullscreen mode

result-4

Multiplication:

var a = 14;
var b = 10;
console.log(a*b);
Enter fullscreen mode Exit fullscreen mode

result-140

Division:

var a = 14;
var b = 10;
console.log(a/b);
Enter fullscreen mode Exit fullscreen mode

result-1

Modulus:

var a = 14;
var b = 10;
console.log(a%b);
Enter fullscreen mode Exit fullscreen mode

result-4

4. Special Numbers in JS :

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 :

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"
Enter fullscreen mode Exit fullscreen mode

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'?
Enter fullscreen mode Exit fullscreen mode

6.Unary and binary Operators :

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
Enter fullscreen mode Exit fullscreen mode

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 :

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

8. Logical Operators :

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

9.Empty values :

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 :

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

Enter fullscreen mode Exit fullscreen mode

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 :

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
Enter fullscreen mode Exit fullscreen mode

&& -> 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
Enter fullscreen mode Exit fullscreen mode


Thankyou for reading!😃
All feedbacks are welcome 🙆‍♀️

Connect with me on :

Top comments (0)