# Common JavaScript Manual/Printable version

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/Common_JavaScript_Manual

# How read this book
## Optional arguments

Optinal variables enclosed in square brackets. For example Array.join([separator])

## Variable in description

In function description variables are bolded. For example Math.sqrt(num) - square root of num

## Undefined number of arguments

Undefined number of arguments in function written as name... and function description name are all these variables in general, and name[n] either name. For example Array.concat(elements...) - returns array that containg elements from Array and elements if elements[n] is array then to the array that returns are added all elements from elements[n].

# Data types - Numbers
## Numbers

For example, let's set the value for the variable 'a'
And for the variable 'b'

### Arithmetic operations

Let's output results of arithmetic operations with these variables.

### Increment, Decrement, Unary operators

For enlarging or reducing some variable by one we can use increment and decrement operator. And we can change char of number by unary operator. Decrement and increment have two types of recording - postfix and prefix. Prefix - first the value is changed then the new value is returned. Postfix - first the value is returned then value is changed.

### Standart library functions

Also we can apply some functions to numbers. We will consider only most important.

### Standart library constants

Standart library include some math constants. We will consider only the most important.

### Special values

Numbers in JavaScript also include two special values are Infinity and NaN(Not a Number)
Data types - Arrays →

# Data types - Arrays
## Making Array

For making array it's better to use list literal because it's most popular and simple syntaxis

## Work with elements

And so, we have an array 'b' with five numbers and the empty array 'a'.
* From array b - [1,2,3,4,5] get element with index 2 (Numeration in arrays starts from 0). Now array a = [2]
* We delete element with index 2 from array b. Now array b = [0,1,undefined,3,4].
* We change value of element with index 3 from array b. Now array b = [0,1,undefined,6,4].

## Work with slices

Array.slice(start,end) - returns array with elements of Array from start index to end index without element with index end.
If start or end is negative number then index of start or end equal (length of array + start or end).
Array.splice(start,number,elem...) - return slice with number of elements from Array from start and it deletes this elements from Array, and it replaces it's by elem

## Stack and row

You can use any Array as stack or row for it there are 4 function.
| Name | Action |
| Array.pop | Delete and return last element of Array |
| Array.push(elem...) | Insert elem to end of Array |
| Array.shift | Delete and return first element of Array |
| Array.unshift(elem...) | Insert elem to start of Array |
For example:

## Sorting and reverse

Also Array.sort([predicate]) - If predicate not defined then sort elements in Array in lexicographical order else sort element in Array by results of function predicate that gets two arguements and returns -1 if first argument less then second or 1 if second argument less then first or 0 if arguments equal.
Array.reverse - reverse elements in Array

## Concatenate and joining

Array.concat(elem1...) - returns array that containg elements from Array and elem if elem[n] is array then to the array that returns are added all elements from elem[n].
Array.join([separator]) - returns string with all elements and separator before every, if separator not defined then separator = ","

## Length of Array

Array.length - number of elements in Array
← Data types - Numbers · Data types - Strings →

# Data types - Strings
## Strings

Let's make a variable with a string as value; we will use string literal. Strings can be enclosed in double and single quotes (though in most languages strings are enclosed in double quotes and chars in single quotes).

### Concatenation

We can concatenate strings using '+' operator. Let's add to string in variable 'a' string "world".

### Length of string

Any string has a 'length' property that contains the number of chars in the string.

### Split

String.split([separator]) - returns array of substrings that was obtained by separating String by separator. If separator is not defined then separator = ","

### Getting substrings and char

We can get a substring from a string using the substr(index,len) and substring(indexA,indexB) methods. First returns the substring from 'index' with length equal to len. Second returns the substring from indexA to indexB.
We can also get a char at some position using charAt function.

### Position of substring

We can also get a position of some substring in a string using the indexOf and lastIndexOf methods. Both functions have two arguments but only the first is required: the string to be searched and the position from that the string is searched. indexOf returns the first position of the substring and lastIndexOf returns the last position of the substring. If the substring is not found both functions return -1.
← Data types - Arrays · Data types - Objects →

# Conditional statements
## If statements

We can do so that some blocks of code should execute only if condition true. For it we should use "if" expression. Let's see example.

## Else statements

We also can make code block that should be executed only if condition false. For execute much commands just enclose it in '{' and '}'.

## Else if statements

Also we can set any number conditions and blocks of code for this conditions.
← Data types - Objects · While and For loops →

# While and For loops
## While

While expression looks so:
Here, if the condition is true then the code inside the loop and then the process goes first but if the condition is true then execution continues after the loop. Let's see example.

## For

"For" cycle is a short form of while.
Let's rewrite our code.
← Conditional statements · Do .. While and For .. in loops →

# Do .. While and For .. in loops
## Do .. While

In this cycle, executing the code in it and then check the condition and if it is true then the loop is executed again, otherwise terminate the loop. Let's see example.
Do .. while loop can be used if code inside a loop set some values and in condition you call some function.

## For .. in

For .. in loop using if need iterate over all properties of object. For example.
In this code on every iteration to variable 'i' set value with key of property.
← While and For loops · Break, continue, labels →

# Break, continue, labels
### Break

Break statement can interrupt the cycle. For example.
This code makes string with list of pairs of numbers while they are not equal.

### Continue

Continue statement makes the transition to the next iteration of the loop.
This script output numbers from array which square roots is integer.

## Labels

Labels help use break and continue in nested loops.
← Do .. While and For .. in loops · Logic and Comparison operators →

# Logic and Comparison operators
## Logic operators

There are three logical operators in JavaScript. It are "not","and","or". Let's see example.
| Operator | Action |
| || | or |
| && | and |
| ! | not |

## Comparison operators

In parts of the previous file you've already seen some of these operators, and certainly understand their meaning but in anyway. There are 8 comparison operators in JavaScript.
| Operator | Condition |
| == | Equal |
| != | Not equal |
| === | Equal value and type |
| !== | Not equal value or type |
| > | More |
| < | Less |
| <= | Less or equal |
| >= | More or equal |
And for example:

## XOR Hack

In JavaScript there is no operator XOR (also named Exclusive OR). But we can write function that use bitwise XOR and return booleadn XOR of two arguments.
And let's use it.
← Break, continue, labels · Condition operator →

# Condition operator
## Conditional operator

If we need just set value for any variable in depent of condition, we can use conditional operator. It's shorter than "if" expression many times. Let's see example.

### Nested conditional operator

As 'if' expression so and Conditional operator can be multiple.
← Logic and Comparison operators · Switch expression →