Reviewing Eloquent Javascript #Chpt1

Reviewing Eloquent Javascript #Chpt1

Decoding JavaScript Essentials: Insights from Eloquent JavaScript Chapter 1

In this blog, I will write on my learnings from the Eloquent Javascript Book's Chapter 1 : Values, Types, and Operators.

Here's the PDF for Eloquent Javascript Book's Chapter 1.

Bits

  • A computer, at the lowest level, stores data in binary, a numeral system in which there are just two digits, 0 and 1 (base 2). But, in our day to day life we count numbers from 1 to 9 which is a decimal number system (base 10).

  • For Example, for us humans we know that 111 is "One hundred and eleven" but if we want to convert this decimal number in binary its value is 01101111

..  256 128 64 32 16 8 4 2 1 
      0   0  1  1  0 1 1 1 1
  • We only have to add values that are above 1 i.e. 64 + 32 + 8 + 4 + 2 + 1 = 111

  • To represent letters, all we need to do is decide how numbers map to letters. For example, the letter “A”, is the number 65, and “B” is 66, and so on.

  • Accent marks, symbols and characters, Images, Videos, Colors, Emojis, GIFs can all be represented using 1's and 0's with a standard called Unicode (one specific version of which is called UTF-8).

  • 8 bits = one byte.

Volatile Memory and Non-Volatile Memory

  • Volatile Memory ( Primary Memory ) is used to store computer programs and data that CPU needs in real time and is erased once computer is switched off. Used for temorary Storage.

    • For Example, RAM and Cache memory are volatile memory.
  • Where as Non-volatile Memory ( Secondary Memory ) is static and remains in the computer even if computer is switched off. Used for Permanent Storage.

    • For Example, ROM and HDD are non-volatile memory.
  • One quick example:

    • When there is no power supplied to a system (computer) the operating system is in secondary memory once the system is on our operating System comes in primary memory i.e. operating system is shifted from secondary to primary memory when the system's power is on.

JavaScript Numbers are Always 64-bit Floating Point

  • In JavaScript we don't define different number types like integers, short, long, floating-point etc. Unlike other programming languages like Java, C and C++.

  • JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.

  • For Example:

       typeof 8778
       typeof 7e45
       typeof 34.56
       typeof 27.76e56
    

    No matter whether your value is an integer number (short or long) or a decimal number, the language exposes just one type to represent all of them i.e. "number".

How the negative numbers are stored in memory in Javascript?

  • When there is a number with minus sign, the number (ignoring minus sign) is converted to its binary format.

  • Then the two’s complement of the number is calculated.

    • For Eg: num = 1001, now convert number into 1's complement i.e. 0110. Now, left-hand-side number will come to right-hand-side i.e 1100.
  • That two’s complement is kept at place allocated in memory and the sign bit will be set to 1 because the binary being kept is of a negative number.

  • Aterwards, when we will be accessing that value firstly the sign bit will be checked if the sign bit is 1 then the binary will be two’s complemented and converted to its decimal number and will be represented with a minus sign.

Operator Precedence

  • Operator precedence means in which order our operations will be exceuted.

  • Most of the time they follow our general BODMAS maths rule that stands for Bracket, Order, Division, Multiplication, Addition, and Subtraction.

  • But when it comes to logical and comparison operators here are the docs that will help you to know the precedence of the operator.

  • For Example:

    
       3 + 17 > 13 - 3 // 20 > 10
       // true
    

    Because addition and subtraction operators has higher precedence than boolean operators

NaN

Not a number NaN is itself is a type of number

  typeof NaN
 'number'

This is the only value in JavaScript that is not equal to itself.

 console.log(NaN == NaN)
 // → false

Infinity

Infinity is an error value indicating one of two problems: a number can’t be represented because its magnitude is too large, or a division by zero has happened.

Infinity is larger than any other number (except NaN). Similarly. -Infinity is smaller than any other number (except NaN). That makes them useful as default value, for example, when you are looking for a minimum or maximum.

Dividing by zero produces Infinity as an error value:

 > 3 / 0
 Infinity
 > 3 / -0
 -Infinity
 > Infinity - Infinity
 NaN
 > Infinity / Infinity
 NaN

Also, I understood one function isFinite() that allows us to check whether a value is an actual number (neither infinite nor NaN):

 > isFinite(5)
 true
 > isFinite(Infinity)
 false
 > isFinite(NaN)
 false

Charater Special Escaping

  • Many programming languages support a concept called Escape Sequence. When a character is preceded by a backslash ( \ ), it is called an Escape Sequence.

  • Example:

    • \b: backspace

    • \t: horizontal tab

    • \v: vertical tab

    • \0: null character

  • If you want to know more about Charater Special Escaping here's a link.

The size of a JavaScript String

  • Pre-ES6 Version: 2 bytes per character

  • ES6 and later Versions: 2 bytes per character, or 5 or more bytes per character. The additional sizes come into play because ES6 (ECMAScript 6) adds support for Unicode code point escapes.

Template literals

  • Template literals are string literals allowing embedded expressions.

  • You can use multi-line strings and string interpolation features with them.

  • Template literals are enclosed by the backtick.These are indicated by the dollar sign and curly braces (${expression}).

  • Quick Example:

    
       let firstName = 'Prerana';
       console.log(`Hello ${firstName}!
       How are you
       today?`);
       // Output:
       // Hello Prerana!
       // How are you
       // today?
    

Unary Operator

  • Operators that use two values are called binary operators, while those that take one are called Unary operators.

  • The minus operator can be used both as a binary operator and as a unary operator.

  • Examples of Unary Operator:

    • Unary plus (+)

     +true  // 1 (Boolean value is converted to a number)
     +false // 0 (Boolean value is converted to a number.)
     let a = '10' //String
     console.log(+a); // 10 (String is converted to a number)
  • Logical Not (!)

     !false  // returns true

This returns True because it will convert the operand ( false ) into it’s boolean value and then negate it.

  • Increment ( ++ ) and Decrement ( -- )

  • Unary negation ( - )

Lexicographical Order

  • To check whether a string is greater than another in JavaScript, Lexicographical Order is used, which simply means that it compares sequentially the elements that have the same position in both strings against each other.
'college' > 'school'
false

Ternary Operator

  • The ternary operator ( shorthand of Conditional Statement ) is the only JavaScript operator that takes three operands.

  • Syntax: condition ? exprIfTrue : exprIfFalse

  • Explanation: the exprIfTrue will exceute if the condition is True and exprIfFalse will execute if condition is false.

    
       let age = prompt('Enter your Age :');
       let result = (age < 18 ) ? 'Teenager' : 'Adult';
       console.log(`You are a ${result}`);
    

    If the user enters age less than 18 then teenager will be printed but if the user enters age as more than aur equal to eighteen then adult will be printed

Difference between null and undefined

  • Null means an empty or non-existent value.

    
       var a = null;
       console.log(a);
       // null
    
  • Undefined means a variable has been declared, but the value of that variable has not yet been defined

    
       var a;
       console.log(a);
       // undefined
    

Type Coercion

  • Type coercion is conversion of values from one data type to another (such as strings to numbers).

  • Type conversion is similar to type coercion because they both convert values from one data type to another with one key difference — type coercion is implicit whereas type conversion can be either implicit (done for the programmer by the interpreter behind the scenes ) or explicit (done by the Programmer).

  • Example: Here, javascript has coerced ( implicit ) the 1 from a number into a string and then concatenated the two values together, resulting in a string of 51.

    
       console.log("5" + 1)
       // 51
    

Short Circuit Evaluation

  • There are three logical operators in JavaScript:

    • Logical AND &&

    • Logical OR ||

    • Logical NOT !

  • Two important aspects of logical operators in JavaScript is that they evaluate from left to right, and they short-circuit.

  • If you want to know more about short circuit evaluations here are the docs.

  • What this means is that when JavaScript evaluates an OR expression ( returns true if either operand is true ) , if the first operand is true, JavaScript with short-circuit and not even look at the second operand.


true || abc
//true

Important Points we should remember:

  • When the types 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.

  • The rules for converting strings and numbers to Boolean values state that 0, NaN, and the empty string ("") count as false, while all the other values count as true.

Yes, So that's all these are my key Learning from the Chapter 1 of Book Eloquent Javascript. Also, Please do share your key learning from the Chapter 1 and what did you understood the most.

This is a Bloging Challenge from #teamtanayejschallenge

Here's a link to the Website: ejs-challenge.netlify.app

References:

MDN Javascript

Javasript Info

Thank you very much for the patience. I’d love to hear your feedback about the post. Let me know what you think about this article, and javascript in general, through my Twitter and LinkedIn handles. I would love to connect with you out there!

Peace!

Did you find this article valuable?

Support precodes by becoming a sponsor. Any amount is appreciated!