JavaScript Crash Course Notes

Note: All the example files from this course are in a file named examples.zip, which you can download at tinyurl.com/jsccexamples There is also an online course for the book at tinyurl.com/jsccourse.

Introduction to JavaScript

Incorporating JavaScript into a web page

Comments

Using semicolons

Where to place the JavaScript code

▲ Top

JavaScript syntax

Syntax is a set of rules that define how to correctly structure a JavaScript program.

Case sensitivity

Whitespace

Variables

Variable naming

Using quotation marks in strings

Escape characters

Variable typing and casting

▲ Top

JavaScript operators

Arithmetic operators

Operator Description Example Result
+ Addition 3 + 11 141
- Subtraction 9 - 4 5
* Multiplication 3 * 4 12
/ Division 21 / 7 3
% Modulus (remainder after division) 21 % 8 5
++ Increment (increase by 1). In preincrementing (the ++ is before the variable, e.g., ++a), the variable is incremented before it is used. In postincrementing (the ++ is after the variable, e.g., a++), the variable is incremented after it is used. a = 5; ++a (a equals) 6
-- Decrement (decrease by 1). This can be preincremented (--a) or postincremented (a--). a = 5; --a (a equals) 4

Arithmetic functions

Function Description
Math.abs(a) Returns a as a positive number (absolute value).
Math.acos(a) Returns the arc cosine of a.
Math.asin(a) Returns the arc sine of a.
Math.atan(a) Returns the arc tangent of a.
Math.atan2(a,b) Returns the arc tangent of a / b.
Math.ceil(a) Rounds up to return the integer closest to a.
Math.cos(a) Returns the cosine of a.
Math.exp(a) Returns the exponent of a (Math.E to the power a).
Math.floor(a) Rounds down to return the integer closest to a.
Math.log(a) Returns the log of a base e.
Math.max(a,b) Returns the maximum of a and b.
Math.min(a,b) Returns the minimum of a and b.
Math.pow(a,b) Returns a to the power b.
Math.random() Returns a random number between 0 and 0.999 (recurring). Example — to emulate a 6-sided dice: Math.ceil(Math.random() * 6) // returns 1-6
Math.round(a) Rounds up or down to return the integer closest to a.
Math.sin(a) Returns the sine of a.
Math.sqrt(a) Returns the square root of a.
Math.tan(a) Returns the tangent of a.

Assignment operators

Note: Variable a already contains the value 21.

Operator Description Example Result
= Simple assignment a = 42 42
+= With addition a += 5 26
-= With subtraction a -= 2 19
*= With multiplication a *= 3 63
/= With division a /= 10 2.1
%= With modulus a %=4 1

Comparison operators

Operator Description Example Result
== Equal to 1 == 1 true
=== Equal in value and type 1 === '1' false
!= Not equal to 1 != 2 true
!== Not equal in value & type 1 !== '1' true
> Greater than 1 > 2 false
< Less than 1 < 2 true
>= Greater than or equal to 1 >= 1 true
<= Less than or equal to 2 <=1 false

Logical operators

Operator Description Example Result
&& And (&& is known as the and operator) 1 == 1 && 2 == 2 true
|| Or (|| is known as the or operator) 1 == 1 || 2 ++ 3 true
! Not (! is known as the not operator) !(1 == 1) false

The ternary operator

Bitwise operators

Bitwise operators act on the individual 0 and 1 bits that make up binary numbers. Only very advanced JavaScript coders should use these.

Operator Description
& Bitwise and
| Bitwise or
^ Bitwise exclusive or
~ Not
<< Left-shift
>> Right-shift
>>> Zero-fill right-shift

Operator precedence

Precedence Operator type Associativity Individual operators
1 member left-to-right .
[]
new right-to-left new
2 function call left-to-right ()
3 increment n/a ++
decrement n/a --
4 logical-not right-to-left !
bitwise not right-to-left ~
unary + (used before an expression to force it to be a number) right-to-left +
unary negation (used before an expression to negate it, i.e., change negative value to positive or vice versa) right-to-left -
typeof right-to-left typeof
void right-to-left void
delete right-to-left delete
5 multiplication left-to-right *
division left-to-right /
modulus left-to-right %
6 addition left-to-right +
subtraction left-to-right -
7 bitwise shift left-to-right <<
>>
>>>
8 relational left-to-right <
<=
>
>=
in left-to-right in
instanceof left-to-right instanceof
9 equality left-to-right ==
!=
===
!==
10 bitwise-and left-to-right &
11 bitwise-xor left-to-right ^
12 bitwise-or left-to-right |
13 logical-and left-to-right &&
14 logical-or left-to-right ||
15 conditional right-to-left ?:
16 assignment right-to-left =
+=
-=
*=
/=
%=
<<=
>>=
>>>=
&=
^=
|=
17 comma left-to-right ,

The with statement

▲ Top

JavaScript arrays

Array names

Creating an array

Assigning values to an array element

Using indexes

Returning values

Using array elements as indexes

Other ways of creating arrays

Using associative arrays

Manipulating arrays

▲ Top

Multidimensional arrays

Creating a two-dimensional array

Accessing a two-dimensional array

Multidimensional associative arrays

▲ Top

JavaScript array functions

Using for (... in ...)

Using concat

Alternative to concat()

Using join()

Using forEach()