Eloquent Javascript Chapter 1

Values, Types, and Operators

Values

Values are chunks that represent pieces of information

There are 6 basic types of values in Javascript

  1. numbers
  2. strings
  3. Booleans
  4. objects
  5. functions
  6. undefined values

Numbers

  1. Numbers are numeric values such as 13
  2. Numbers in Javascript are made up of 64bits which is about 18 quintillion.
  3. Negative numbers and nonwhole numbers use bits which shrink the maximum number available.

  4. Negative numbers use a bit to display the sign

  5. Decimal numbers use some bits to store the position of the decimal point so the actual whole number is around 9 quadrillion (15 zeros).

  6. Decimal numbers are written with a dot.  9.81

     

Numbers

For small or big numbers you can use scientific notation: 

2.998e8 = 2.998 × 108 = 299,800,000

Calculations with fractional numbers are generally not precise given the 64 bit limit.

Treat fractional digital numbers as approximations, not as precise values

Arithmetic

The + and * symbols are called operators

There is a precedence of the operators

* / + -

% is the remainder (modulo) operator

It returns the  remainder of a division:

> 100 + 4 * 11
< 144
> 314 / 100
< 3
> 314 % 100
< 14

3 Special Numbers

Infinity and -Infinity

Infinity - 1 is still Infinity

NaN stands for “not a number”

Any number of other numeric operations that don’t yield a precise, meaningful result.

> 0/1
< 0
> 1/0
< Infinity
> 0/0
< NaN
> Infinity - 1
< Infinity

Strings

Strings are text wrapped in quotes.

> "Patch my boat with chewing gum"
> 'Monkeys wave goodbye'
> "This is the first line\nAnd this is the second"
< "This is the first line
  And this is the second"

Special characters are escaped using the \ such as \n for a newline.

If you actually need a backslash then you add two backslashes. \\

Unary Operators

Not all operators are symbols such as * or /.

Some are written as words.

typeof is one of those

> console.log(typeof 4.5)
< number
> console.log(typeof "x")
< string

Unary operators take 1 params

Binary operators take 2 params

Boolean Values

Often, you will need a value that simply distinguishes between two possibilities:

“yes” and “no” or “on” and “off”

 

JavaScript has a Boolean type, which has just two values: true and false

Boolean Values

> console.log(3 > 2)
< true
> console.log(3 < 2)
< false

Comparison

The > and < signs are binary operators that return a Boolean value.

Other similar operators are >= (greater than or equal to), <= (less than or equal to), == (equal to), and != (not equal to).

Boolean Values

 

Logical operators

JavaScript supports three logical operators: 

and (&&)or (||), and not (!)

> true && false
< false
> true && true
< true
> false || true
< true
> false || false
< false
> !true 
< false 
> !false
< true

Boolean Values

 

ternary operator

JavaScript supports a conditional or ternary operator: 

: ?

> true ? 1 : 2
< 1
> false ? 1 : 2
< 2

Undefined Values

 

There are two special values, null and undefined, that are used to denote the absence of a meaningful value. 

Many operations in the language that don’t produce a meaningful value (you’ll see some later) yield undefined simply because they have to yield some value.

 

> var test;
< undefined

Automatic Type Conversion

> 8 * null
< 0
> "5" - 1
<  4
> "5" + 1
< 51
> "five" * 2
< NaN
> false == 0
< true

JavaScript goes out of its way to accept almost any program you give it, even programs that do odd things

JavaScript will quietly convert that value to the type it wants, using a set of rules that often aren’t what you want or expect.

This is called type coercion.

Automatic Type Conversion

> null == undefined
< true
> null == 0
< false

However, when null or undefined occurs on either side of the operator, it produces true only if both sides are one of null or undefined.

This is useful if you want to test to see if a value has a real value instead null or undefined.

> var val;
> val == null
< true

Automatic Type Conversion

How do you determine false?

> 0 == false
< true
> "" == false
< true

0NaN, and the empty string ("") count as false.

If you don't want automatic type conversion you can use === and !==.

It is highly recommended that you use === and !== to prevent unexpected type conversions.

> 0 === false
< false
> "" === false
< false

Short-Circuiting of Logical Operators

&& and || operators will convert the value on the left side to a boolean in order to decide what to do then they return either the original left hand value or the right hand value.

> true || x
< true
> false && x
< false
> null || "user"
< "user"
> "Karl" || "user"
< "Karl"
> "" || "user"
< "user"

Summary

 

4 types of values

  • Numbers
  • Strings
  • Booleans
  • undefined

Created by name: true, null

Created by value: 13, "abc"

Summary

 

Can combine and transform values with operators

binary operators for arithmetic (+-*/, and %)

string concatenation (+)

comparison (==, !====!==<><=>=)

logic (&&||)

unary operators (-, !, typeof)

ternary operators ( ? : )

Eloquent Javascript Chapter 1

By Dustin McCraw

Eloquent Javascript Chapter 1

  • 1,316