Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 10, 2020 04:19 am GMT

Eloquent Javascript - Chapt 1

In this article I will retrospect the first chapter of the book "Eloquent Javascript"

Table of Contents:

  1. Bits
  2. Values
  3. Numbers
  4. Precedence of the operator
  5. Special Numbers
  6. Strings and Character Escaping
  7. Ternary operator
  8. Difference between null and undefined (Empty values)
  9. Type coercion
  10. Short-circuit operators

Bits

In this computer world we are enveloped with the only data, and data comprises of Bits.
In simple words, bits are the combination of zeros and ones which we also called as the decimal number system.

For example: If we want to demonstrate "95" in bits which we want to convert into decimal number then its binary value would be 01011111

..  256 128 64 32 16 8 4 2 1       0   0  1  0  1 1 1 1 1
Enter fullscreen mode Exit fullscreen mode

And its non-zero digits would be 64+16+8+4+2+1 which adds upto 95.

  • 8 bits represents 1 byte.

Values

Volatile and Non-volatile Memory

Volatile Memory requires power to hold onto the stored information. It is a temporary storage where all the information is erased when the computer is switched off. It is also called as primary memory.
Example: RAM, HDD, SSD etc.

Non-volatile memory is called as the permanent storage and which do not require power for the information to be static. Even if the computer switches off the information remains unvarying. It is also said to be secondary memory.

  • Values in simple words is the ocean of bits which is often divided into chunks to store preventing the loss.

  • In Javascript just call a value and you have one.

Numbers

  • Javascript has a fixed 64 bits to store a single number value.
  • Javascript does not have types of numbers as in other programming languages.

Can we store Negative numbers in that bit?

  • The answer is YES!, one bit has the sign of that number. Bit firstly it will be converted into the binary format and then its 2's complement will be taken into consideration.

  • Also the Fractional numbers can be stored, but they are stored in the form of dot. the calculation of the fractional numbers would not be always precise.
    example: 39.48

  • In numbers we can also scientific notation.
    example: 2.458e9
    where e(for exponent) followed by the exponent of the number.

Precedence of the Operators

  • Precedence of the operators means how and in which order the arithmetic operations will be executed.
  • The precedence is followed by the common rule of BODMAS which stands for Bracket Order Division Multiplication Addition Subtraction.
  • When there are same precedence operators in the problem then it is executed from left to right.
  • %(remainder or modulo) have the same precedence as of Multiplication and division.

Special Numbers

Special Numbers have three special values in JS.

  • Infinity and Negative InfinityThe infinity comes into play when the magnitude of the operation is much larger or something is divided by 0.

The initial value of value of infinity(positive infinity) is greater than any other number
The initial value of Negative Infinity is smaller than any other number.

> infinity + 1infinity> 10^1000infinity> log(0)- infinity> 1 / infinityinfinity> 1 / 0infinity
Enter fullscreen mode Exit fullscreen mode

We can use it using the following commands:
Number.NEGATIVE_INFINITY
Number.POSITIVE_INFINITY.

  • NaN
  • NaN(Not a Number) is itself a numberThe following expressions results into NaN.
> sqrt(-2)NaN> 8**NaNNaN> 0*infinity NaN> undefined + undefinedNaN
Enter fullscreen mode Exit fullscreen mode
  • NaN is the only number that is not equal to itself.

Strings and Character Escaping

  • Strings can be enclosed in quotes such as
    single quotes, double quotes, backticks

  • The escaping character is used using backslash(). \ is called as escape sequence.

\b: backspace
\f: form feed

: new line
: horizontal tab
\v: vertical tab

Comparison and Logical Operators

  • Comparison operators are used to determine equality or difference between different values.

  • They are binary operators. In JavaScript when we compare strings the characters goes from left to right, based on the Unicode convention.

  • Comparison operators includes:

    • == Equal to
    • === Equal value/type
    • != not equal
    • !== not equal value/type
    • > Greater than
    • < Less than
    • >= Greater than or equals to
    • <= Less than or equals to
  • JavaScript offers three logical operators

    • && and
    • || or
    • ! not

Ternary Operator

  • Ternary operator in JavaScript contains three operators

Syntax: condition ? valisTru : valisFal

Example:

var status = (age >= 18) ? 'adult' : 'minor';
Enter fullscreen mode Exit fullscreen mode

If the condition is true then valisTru will be executed otherwise valisFal.

Difference between null and undefined(Empty values)

  • These are values but they contain no information.

  • Undefined is when the value have not been assigned to the variable.

var(c);console.log(c);// undefined
Enter fullscreen mode Exit fullscreen mode
  • Null is the value which is empty or which does not exists.
var c = null;console.log(c);// null
Enter fullscreen mode Exit fullscreen mode

Type coercion

  • In JavaScript if we assign wrong value to the operator then it automatically assigns the value which we do not expect without giving any error,
    This is called as type coercion.

  • Examples:
    When something do not map in a number in this case "Six" and is converted into number will result into NaN.

> console.log("six" * 2);NaN
Enter fullscreen mode Exit fullscreen mode
  • Arithmetic operation will keep producing NaN on operations on NaN.
  • Type conversion is similar to type coercion because they convert one data type to another but the difference is Type coercion is only implicit but type conversion can be implicit or explicit.

Short-circuit Evaluation

  • The logical operators i.e. || and && evaluate from left to right and they short-circuit.

  • Example:
    When the below command is executed the OR operation return true if the statement is true.
    If the first operand is true then the JavaScript short-circuit do not move forward to the second operand.

console.log("Agnes" || "user")//  Agnes
Enter fullscreen mode Exit fullscreen mode

So this is it! This article was my review of the first chapter of the Eloquent JavaScript Chapter-1 and what I learned from it.

This is Blogging Challenge from #teamtanayejschallenge

Thankyou for coming this far. I hope this helps.
Enjoy reading.


Original Link: https://dev.to/binalmehta/eloquent-javascript-chapt-1-5bcc

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To