A JavaScript string stores a series of characters like "John Doe".
A string can be any text inside double or single quotes:
var carname = "Volvo XC60";
var carname = 'Volvo XC60';
String indexes are zero-based: The first character is in position 0, the second in 1, and so on.
For a tutorial about Strings, read our JavaScript String Tutorial.
Primitive values, like "John Doe", cannot have properties or methods (because they are not objects).
But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.
Property |
Description |
Returns the string's constructor function |
|
Returns the length of a string |
|
Allows you to add properties and methods to an object |
Method |
Description |
Returns the character at the specified index (position) |
|
Returns the Unicode of the character at the specified index |
|
Joins two or more strings, and returns a new joined strings |
|
Converts Unicode values to characters |
|
Returns the position of the first found occurrence of a specified value in a string |
|
Returns the position of the last found occurrence of a specified value in a string |
|
Compares two strings in the current locale |
|
Searches a string for a match against a regular expression, and returns the matches |
|
Searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced |
|
Searches a string for a specified value, or regular expression, and returns the position of the match |
|
Extracts a part of a string and returns a new string |
|
Splits a string into an array of substrings |
|
Extracts the characters from a string, beginning at a specified start position, and through the specified number of character |
|
Extracts the characters from a string, between two specified indices |
|
Converts a string to lowercase letters, according to the host's locale |
|
Converts a string to uppercase letters, according to the host's locale |
|
Converts a string to lowercase letters |
|
Returns the value of a String object |
|
Converts a string to uppercase letters |
|
Removes whitespace from both ends of a string |
|
Returns the primitive value of a String object |
The HTML wrapper methods return the string wrapped inside the appropriate HTML tag.
These are not standard methods, and may not work as expected in all browsers.
Method |
Description |
Creates an anchor |
|
Displays a string using a big font |
|
Displays a blinking string |
|
Displays a string in bold |
|
Displays a string using a fixed-pitch font |
|
Displays a string using a specified color |
|
Displays a string using a specified size |
|
Displays a string in italic |
|
Displays a string as a hyperlink |
|
Displays a string using a small font |
|
Displays a string with a strikethrough |
|
Displays a string as subscript text |
|
Displays a string as superscript text |
JavaScript has only one type of number.
Numbers can be written with, or without, decimals:
var x = 3.14; // A number with decimals
var y = 34; // A number without decimals
Extra large or extra small numbers can be written with scientific (exponent) notation:
var x = 123e5; // 12300000
var y = 123e-5; // 0.00123
For a tutorial about JavaScript numbers, read our JavaScript Number Tutorial.
Property |
Description |
Returns the function that created JavaScript's Number prototype |
|
Returns the largest number possible in JavaScript |
|
Returns the smallest number possible in JavaScript |
|
Represents negative infinity (returned on overflow) |
|
Represents a "Not-a-Number" value |
|
Represents infinity (returned on overflow) |
|
Allows you to add properties and methods to an object |
Method |
Description |
Converts a number into an exponential notation |
|
Formats a number with x numbers of digits after the decimal point |
|
Formats a number to x length |
|
Converts a number to a string |
|
Returns the primitive value of a number |
Arithmetic operators are used to perform arithmetic between variables and/or values.
Given that y = 5, the table below explains the arithmetic operators:
Operator |
Description |
Example |
Result in y |
Result in x |
Try it |
+ |
Addition |
x = y + 2 |
y = 5 |
x = 7 |
|
- |
Subtraction |
x = y - 2 |
y = 5 |
x = 3 |
|
* |
Multiplication |
x = y * 2 |
y = 5 |
x = 10 |
|
/ |
Division |
x = y / 2 |
y = 5 |
x = 2.5 |
|
% |
Modulus (division remainder) |
x = y % 2 |
y = 5 |
x = 1 |
|
++ |
Increment |
x = ++y |
y = 6 |
x = 6 |
|
x = y++ |
y = 6 |
x = 5 |
|||
-- |
Decrement |
x = --y |
y = 4 |
x = 4 |
|
x = y-- |
y = 4 |
x = 5 |
For a tutorial about arithmetic operators, read our JavaScript Operators Tutorial.
Assignment operators are used to assign values to JavaScript variables.
Given that x = 10 and y = 5, the table below explains the assignment operators:
Operator |
Example |
Same As |
Result in x |
Try it |
= |
x = y |
x = y |
x = 5 |
|
+= |
x += y |
x = x + y |
x = 15 |
|
-= |
x -= y |
x = x - y |
x = 5 |
|
*= |
x *= y |
x = x * y |
x = 50 |
|
/= |
x /= y |
x = x / y |
x = 2 |
|
%= |
x %= y |
x = x % y |
x = 0 |
For a tutorial about assignment operators, read our JavaScript Operators Tutorial.
The + operator, and the += operator can also be used to concatenate (add) strings.
Given that text1 = "Good ", text2 = "Morning", and text3 = "", the table below explains the operators:
Operator |
Example |
text1 |
text2 |
text3 |
Try it |
+ |
text3 = text1 + text2 |
"Good " |
"Morning" |
"Good Morning" |
|
+= |
text1 += text2 |
"Good Morning" |
"Morning" |
"" |
Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given that x=5, the table below explains the comparison operators:
Operator |
Description |
Comparing |
Returns |
Try it |
== |
equal to |
x == 8 |
false |
|
x == 5 |
true |
|||
=== |
equal value and equal type |
x === "5" |
false |
|
x === 5 |
true |
|||
!= |
not equal |
x != 8 |
true |
|
!== |
not equal value or not equal type |
x !== "5" |
true |
|
x !== 5 |
false |
|||
> |
greater than |
x > 8 |
false |
|
< |
less than |
x < 8 |
true |
|
>= |
greater than or equal to |
x >= 8 |
false |
|
<= |
less than or equal to |
x <= 8 |
true |
For a tutorial about comparison operators, read our JavaScript Comparisons Tutorial.
The conditional operator assigns a value to a variable based on a condition.
Syntax |
Example |
Try it |
variablename = (condition) ? value1:value2 |
voteable = (age < 18) ? "Too young":"Old enough"; |
Example explaied: If the variable "age" is a value below 18, the value of the variable "voteable" will be "Too young", otherwise the value of voteable will be "Old enough".
Logical operators are used to determine the logic between variables or values.
Given that x=6 and y=3, the table below explains the logical operators:
Operator |
Description |
Example |
&& |
and |
(x < 10 && y > 1) is true |
|| |
or |
(x == 5 || y == 5) is false |
! |
not |
!(x == y) is true |
Bit operators work on 32 bits numbers. Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to a JavaScript number.
Operator |
Description |
Example |
Same as |
Result |
Decimal |
& |
AND |
x = 5 & 1 |
0101 & 0001 |
0001 |
1 |
| |
OR |
x = 5 | 1 |
0101 | 0001 |
0101 |
5 |
~ |
NOT |
x = ~ 5 |
~0101 |
1010 |
10 |
^ |
XOR |
x = 5 ^ 1 |
0101 ^ 0001 |
0100 |
4 |
<< |
Left shift |
x = 5 << 1 |
0101 << 1 |
1010 |
10 |
>> |
Right shift |
x = 5 >> 1 |
0101 >> 1 |
0010 |
2 |
In HTML, JavaScript statements are "commands" to the browser.
The purpose, of the statement, is to tell the browser what to do.
This JavaScript statement tells the browser to write "Hello Dolly" inside an HTML element identified with id="demo":
document.getElementById("demo").innerHTML = "Hello Dolly.";
For a tutorial about Statements, read our JavaScript Statements Tutorial.
JavaScript statements often start with a statement identifier to identify the JavaScript action to be performed.
Statement identifiers are reserved words and cannot be used as variable names (or any other things).
The following table lists all JavaScript statements:
Statement |
Description |
Exits a switch or a loop |
|
Breaks one iteration (in the loop) if a specified condition occurs, and continues with the next iteration in the loop |
|
Stops the execution of JavaScript, and calls (if available) the debugging function |
|
Executes a block of statements and repeats the block while a condition is true |
|
Marks a block of statements to be executed as long as a condition is true |
|
Marks a block of statements to be executed for each element of an object (or array) |
|
Declares a function |
|
Marks a block of statements to be executed depending on a condition |
|
Stops the execution of a function and returns a value from that function |
|
Marks a block of statements to be executed depending on different cases |
|
Throws (generates) an error |
|
Marks the block of statements to be executed when an error occurs in a try block, and implements error handling |
|
Declares a variable |
|
Marks a block of statements to be executed while a condition is true |
The Math object allows you to perform mathematical tasks.
Math is not a constructor. All properties/methods of Math can be called by using Math as an object, without creating it.
var x = Math.PI; // Returns PI
var y = Math.sqrt(16); // Returns the square root of 16
For a tutorial about the Math object, read our JavaScript Math Tutorial.
Property |
Description |
Returns Euler's number (approx. 2.718) |
|
Returns the natural logarithm of 2 (approx. 0.693) |
|
Returns the natural logarithm of 10 (approx. 2.302) |
|
Returns the base-2 logarithm of E (approx. 1.442) |
|
Returns the base-10 logarithm of E (approx. 0.434) |
|
Returns PI (approx. 3.14) |
|
Returns the square root of 1/2 (approx. 0.707) |
|
Returns the square root of 2 (approx. 1.414) |
Method |
Description |
Returns the absolute value of x |
|
Returns the arccosine of x, in radians |
|
Returns the arcsine of x, in radians |
|
Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians |
|
Returns the arctangent of the quotient of its arguments |
|
Returns x, rounded upwards to the nearest integer |
|
Returns the cosine of x (x is in radians) |
|
Returns the value of Ex |
|
Returns x, rounded downwards to the nearest integer |
|
Returns the natural logarithm (base E) of x |
|
Returns the number with the highest value |
|
Returns the number with the lowest value |
|
Returns the value of x to the power of y |
|
Returns a random number between 0 and 1 |
|
Rounds x to the nearest integer |
|
Returns the sine of x (x is in radians) |
|
Returns the square root of x |
|
Returns the tangent of an angle |
The Date object is used to work with dates and times.
Date objects are created with new Date().
There are four ways of instantiating a date:
var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
For a tutorial about date and times, read our JavaScript Date Tutorial.
Property |
Description |
Returns the function that created the Date object's prototype |
|
Allows you to add properties and methods to an object |
Method |
Description |
Returns the day of the month (from 1-31) |
|
Returns the day of the week (from 0-6) |
|
Returns the year (four digits) |
|
Returns the hour (from 0-23) |
|
Returns the milliseconds (from 0-999) |
|
Returns the minutes (from 0-59) |
|
Returns the month (from 0-11) |
|
Returns the seconds (from 0-59) |
|
Returns the number of milliseconds since midnight Jan 1, 1970 |
|
Returns the time difference between UTC time and local time, in minutes |
|
Returns the day of the month, according to universal time (from 1-31) |
|
Returns the day of the week, according to universal time (from 0-6) |
|
Returns the year, according to universal time (four digits) |
|
Returns the hour, according to universal time (from 0-23) |
|
Returns the milliseconds, according to universal time (from 0-999) |
|
Returns the minutes, according to universal time (from 0-59) |
|
Returns the month, according to universal time (from 0-11) |
|
Returns the seconds, according to universal time (from 0-59) |
|
getYear() |
Deprecated. Use the getFullYear() method instead |
Parses a date string and returns the number of milliseconds since January 1, 1970 |
|
Sets the day of the month of a date object |
|
Sets the year (four digits) of a date object |
|
Sets the hour of a date object |
|
Sets the milliseconds of a date object |
|
Set the minutes of a date object |
|
Sets the month of a date object |
|
Sets the seconds of a date object |
|
Sets a date to a specified number of milliseconds after/before January 1, 1970 |
|
Sets the day of the month of a date object, according to universal time |
|
Sets the year of a date object, according to universal time (four digits) |
|
Sets the hour of a date object, according to universal time |
|
Sets the milliseconds of a date object, according to universal time |
|
Set the minutes of a date object, according to universal time |
|
Sets the month of a date object, according to universal time |
|
Set the seconds of a date object, according to universal time |
|
setYear() |
Deprecated. Use the setFullYear() method instead |
Converts the date portion of a Date object into a readable string |
|
toGMTString() |
Deprecated. Use the toUTCString() method instead |
Returns the date as a string, using the ISO standard |
|
Returns the date as a string, formatted as a JSON date |
|