JavaScript/Primitive data types

Primitive types use a fixed format; some can contain only a limited number of certain values. In contrast, objects are more complex, especially including methods and properties.

With the exception of  and , primitive types have a corresponding object wrapper with data type specific methods. Therefore you will find on this page descriptions of some methods.

String
String is a datatype to hold text of arbitrary length. String variables are created by assigning a string literal to them. String literals can be enclosed in  or.

If your string literal contains a  or , you can use the other one as the outer delimiter, or you escape them with a.

If your string literal is computed out of some fixed text plus some dynamic parts, you can use the template literal technique. Here, the literal is enclosed in backticks  and contains variables and expressions.

The  operator concatenates two strings, e.g.  . Additionally, there are a lot of methods for strings.

Hint: JavaScript doesn't have something like a 'character' or 'byte' data type.

Properties and methods for strings
We show some methods which are often used. For a complete list, please refer to MDN.

length
is a property, not a method. Hence there are no parenthesizes. It returns the length of the string as a whole number.

concat(text)
The method returns a string where 'text' is appended to the original string.

indexOf(searchText)
The method returns the position of the first occurrence of 'searchText', starting with 0. If 'searchText' cannot be found, -1 is returned. The method is case sensitive.

lastIndexOf(searchText)
The method returns the position of the last occurrence of 'searchText'. If 'searchText' cannot be found, -1 is returned. The method is case sensitive.

replace(text, newtext)
The method returns a string where 'text' is replaced by 'NewText' on the original string. Only the first occurrence is replaced. The method is case sensitive.

As you can see, the  method only returns the new content and does not modify the origin string in 'foo'.

slice(start [, end])
The method returns a substring beginning at the 'start' position.

When the 'end' is provided, they are extracted up to, but not including the end position.

allows extracting text referenced from the end of the string by using negative indexing.

Unlike, the slice method never swaps the 'start' and 'end' positions. If the 'start' is after the 'end',  will attempt to extract the content as presented, but will most likely provide unexpected results.

substr(start [, number of characters])
The method is deprecated. Use  or   instead.

substring(start [, end])
The method extracts a substring starting at the 'start' position.

When the 'end' is provided, they are extracted up to, but not including the end position.

always works from left to right. If the 'start' position is larger than the 'end' position,  will swap the values; although sometimes useful, this is not always what you want; different behavior is provided by #|slice.

toLowerCase
The method returns the current string in lower case.

toUpperCase
The method returns the current string in upper case.

Number
Number is one of the two numeric types (the other one is BigInt). Number stores integer values as well as floating point values in a unified 64-bit format defined by IEEE 754. That means, that JavaScript doesn't have different data types for integers and float like some other languages.

The possible range for such values is approximate -10300 to +10300 with different precision depending on the distance to 0.

In the range from -(253 − 1) to +253 − 1 there is no uncertainness for integer operations. 253 = 9,007,199,254,740,992 which is a little smaller than 1016.

For Number the usual arithmetic operators ('power' is  and 'modulo' is  ), comparison operators (,  , ...), and bitwise operators are available.

In opposite to some other languages, the division of two whole numbers can result in a number with decimal places, e.g..

Properties and methods for numbers
Working with numbers is supported by many properties and methods. Internally, they are implemented at different areas:
 * The built-in object  provides properties that represent common mathematical constants like π or e. Syntax:   (no parenthesis)
 * The build-in object  provides common mathematical functions like sin or log. Syntax:   (with parenthesis)
 * The object  provides properties that characterize the implementation of the data type number, like MAX_VALUE or NEGATIVE_INFINITY. Syntax:   (no parenthesis)
 * The object  provides static methods that check the relation between numbers and other data types, e.g., isInteger or parseFloat. Syntax:   (with parenthesis)
 * The object  provides instance methods that act on concrete number values or variables, e.g., toExponential or toFixed. Syntax:   (with parenthesis)

We show some properties and methods which are often used. For a complete list, please refer to MDN Math and MDN Numbers.

Properties
Most commonly used constants:
 * Returns the constant e.
 * Returns the constant pi.
 * Returns the natural logarithm of 10.
 * Returns the natural logarithm of 2.
 * Returns the square root of 2.

Math.ceil(number)
Returns the smallest integer greater than the number passed as an argument.

Math.floor(number)
Returns the greatest integer less than the number passed as an argument.

Math.round(number)
Returns the closest integer to the number passed as an argument.

Math.max(number_1, number_2)
Returns the higher number from the two numbers passed as arguments.

Math.min(number_1, number_2)
Returns the lower number from the two numbers passed as arguments.

Math.random
Generates a pseudo-random number between 0 and 1.

Number.parseFloat(string)
The two methods  and   convert strings into numbers. They scan the given string from left to right. When they recognize a character distinct from 0 - 9, they finish scanning and return the converted numbers read so far (parseFloat accepts the decimal point). If the first character is distinct from 0 - 9, they return Math.NaN, meaning Not a Number.

Hint: It's not necessary to specify 'Number.' in the source code.

BigInt
BigInt is a data type that represents integers of arbitrary length. Hence, it's a variable-length format (conceptually) delimited only by the available RAM.

BigInts are created either by adding a 'n' to the end of an integer literal or by using the  function.

BigInt supports the arithmetic operators, comparison operators, and most of the bitwise operators.

Boolean
Boolean variables can contain one of two possible values,  or. JavaScript represents  by either a Boolean false, the number 0, NaN, an empty string, or the built-in types undefined or null. Any other values are treated as.

Undefined
Variables that have just been declared but not initialized with any value have the data type undefined.

Null
In JavaScript, null is marked as one of the primitive values, because its behavior is seemingly primitive. However, when using the  operator, it returns "object". This is considered a bug, but one which cannot be fixed because it will break too many scripts.

Symbol
Symbol represents a unique identifier. Symbols may have a descriptor that is given as a string to its constructor.

Symbols are used as keys in objects (embedded in ) to guarantee uniqueness.

Exercises

 * ... are available on another page (click here).