Value Types Eloquent JavaScript

Eloquent JavaScript Chapter 1: Value Types & Operators

Eloquent JavaScript Second Edition by Marijn Haverbeke is one of the most popular JavaScript textbook recommended and used by many JavaScript programmer. In this series, I will read this book for you chapter by chapter, and summarize the most important information so that you can finish the book in much less time and efforts.

CodingZack is a YouTube channel that I am working on. In this channel, I read the most classic and popular IT books for programmers. After reading each chapter, I will create a video to summarize the key information. And you can finish the book by watch these videos. It should be able to save you a lot of time.

Here is the most important information from Chapter 1: VALUES, TYPES, AND OPERATORS in Eloquent JavaScript.

Here is my video for this chapter:

Everything is data in computer.
A chunk of data is called a value.

6 basic types of values in JavaScript:

  1. Numbers
  2. Strings
  3. Boolean
  4. Object
  5. Function
  6. Undefined value

1. Numbers

1. How to present
123
3.8
1.53e8 => 153,000,000

2. Arithmetic
3 + 5 * 4
(3 + 5) * 4
6 / 3
7 % 3

**Fractional digital numbers are approximations, not precise values.

3. Special Numbers
Infinity
-Infinity
NaN: Not a Number (0 / 0)

2. Strings

1. Within single or double quotes: “Zack”

2. Escaping: \

Indicates that the character after it has a special meaning.

\n => new line
\t => tab

Consider:
\”\\n\”.”

3. Concatenates: +

“com” + “bina” + “tion”

Unary operator and binary operator

console.log(typeof 123)
> Number
console.log(typeof “abc”)
> String

Consider:
console.log(-(10-5))

3. Boolean

1. Only two values: true, false

2. Comparison

console.log(5 > 4)
> true
console.log(5 < 4) > false

3. String comparison
Based on Unicode standard

lowercase > uppercase > number(in quotes as strings) > special characters (!#$)

https://en.wikipedia.org/wiki/List_of_Unicode_characters

console.log(“a” > “A”)
console.log(“A” > “1”)

console.log(“abc” < “abd”) > true
console.log(“abc” < “abcd”) > true

4. More comparison operators
>= greater than or equal to
<= less than or equal to
!= not equal to
== equal to

console.log(“Zack” != “Zach”)
> true

5. NaN != NaN

console.log(NaN == NaN)
> false

6. Logical Operator

&& and: only true when both are true
|| or: true when one of them is true
! not: opposite value

console.log(true && false)
> false
console.log(true && true)
> true

console.log(false || true)
> true
console.log(false || false)
>false

console.log(!true)
>false

7. Priority
Lowest: || < && < Comparison(<,>, == and so on) < others

ternary operator

conditional operator

console.log(true ? 1 : 2);
> 1
console.log(false ? 1 : 2);
>2

The value before the question is the condition.
If the condition is true, use the first value.
If the condition is false, use the second value.

4. Undefined Value

undefined: not defined
null: empty

Automatic Type Conversion

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

console.log(null == undefined)
> true
console.log(null == 0)
> false

Compare something to null with == or != to check if it has a real value rather than null or undefined.

0, NaN and “”(the empty string) equal to false, everything else equals to true.

To avoid auto conversion, use === or !=== for precise comparison

console.log(“” === false)
> false

It’s suggested to use === and !==

Short-Circuiting of Logical Operators

||: if the lefthand side is true, the righthand side will be ignored, return the righthand side right away.

console.log(null || “user”)
> user
console.log(“Karl” || “user”)
> Karl

&&: if the lefthand side is false, return the lefthand side right away.

The conditional operator(?:): the second or the third value, unpicked one will not be evaluated.

Summary

Value Types:

  1. Numbers
  2. Strings
  3. Boolean
  4. Undifined

Other two: object and function

Operators

Unary:
– : minus
! : not
typeof : find the type of the value

Binary:
Arithmetic: + – * / %
Comparison: == != === !=== < > <= >=
Logical && ||

Ternary:
? :

NEXT: Chapter 2 PROGRAM STRUCTURE

The next chapter of Eloquent JavaScript is about program structure. We will talk about expressions, statements, variables, function and much much more new concepts about JavaScript and programming.

Tagged with: , , ,

No comment yet, add your voice below!


Leave a Reply