JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.
This format stores numbers in 64 bits,
where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63
Integers (numbers without a period or exponent notation) are considered accurate up to 15 digits.
The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:
To solve the problem above, it helps to multiply and divide:
You can use the toString() method to output numbers as base 16 (hex), base 8 (octal), or base 2 (binary).
Infinity (or -Infinity) is the value JavaScript will return if you calculate a number outside the largest possible number.
Division by 0 (zero) also generates Infinity.
Infinity is a number: typeof Infinity returns number.
NaN: Not a Number
NaN is a JavaScript reserved word indicating that a number is not a legal number.
You can use the global JavaScript function isNaN() to find out if a value is a number.
Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number).
However, if the string contains a numeric value , the result will be a number:
Watch out for NaN. If you use NaN in a mathematical operation, the result will also be NaN.
Or the result might be a concatenation:
The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string and
the lastIndexOf() method returns the index of the last occurrence of a specified text in a string:
Both the indexOf(), and the lastIndexOf() methods return -1 if the text is not found.
Both methods accept a second parameter as the starting position for the search:
The search() method searches a string for a specified value and returns the position of the match:
indexOf() and search() are quite equal.
These are the differences between indexOf() and search():
The search() method cannot take a second start position argument.
The search() method can take much more powerful search values (regular expressions).
There are 3 methods for extracting a part of a string:
slice(start, end)
substring(start, end)
substring() is similar to slice().
The difference is that substring() cannot accept negative indexes.
substr(start, length)
substr() is similar to slice().
The difference is that the second parameter specifies the length of the extracted part.
If you omit the second parameter, substr() will slice out the rest of the string.
If you omit the second parameter, the method will slice out the rest of the string:
Negative positions do not work in Internet Explorer 8 and earlier.
The replace() method replaces a specified value with another value in a string.
By default, the replace() function replaces only the first match:
To replace all matches, use a regular expression with a /g flag (global match):
By default, the replace() function is case sensitive. Writing MICROSOFT (with upper-case) will not work.
To replace case insensitive, use a regular expression with an /i flag (insensitive):
The replace() method does not change the string it is called on. It returns a new string.
A string is converted to upper case with toUpperCase().
A string is converted to lower case with toLowerCase().
There are 2 safe methods for extracting string characters:
charAt(position)
charCodeAt(position)
unsafe method:
This is unsafe and unpredictable:
It does not work in all browsers (not in IE5, IE6, IE7)
It makes strings look like arrays (but they are not)
str[0] = “H” does not give an error (but does not work)
If you want to read a string as an array, convert it to an array first.
A string can be converted to an array with the split() method: