# 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 null and undefined, 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. alert("Hello " + "world!");. 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

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

### includes(searchText)

The method returns true if the string contains a specified string, otherwise it returns false.

### 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 acts case sensitive.

### search(regularExpression)

The method compares the string with a regular expression (=pattern), returning the index of the first match in the string. It is similar to indexOf, but much more powerful. E.g.: it can be made case insensitive by replacing the quotation marks in "hello" with forward slashes and an i at the end.

### lastIndexOf(searchText)

The method returns the position of the last occurrence of 'searchText'. If 'searchText' cannot be found, -1 is returned. The method acts 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 acts case sensitive.
As you can see, the replace 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.
slice allows extracting text referenced from the end of the string by using negative indexing.
Unlike substring, the slice method never swaps the 'start' and 'end' positions. If the 'start' is after the 'end', slice 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 substring or slice 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.
substring always works from left to right. If the 'start' position is larger than the 'end' position, substring 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 -10$^{300}$ to +10$^{300}$ with different precision depending on the distance to 0.
In the range from -(2$^{53}$ − 1) to +2$^{53}$ − 1 there is no uncertainness for integer operations. 2$^{53}$ = 9,007,199,254,740,992 which is a little smaller than 10$^{16}$.
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. alert(1/3);.

## 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 Math provides properties that represent common mathematical constants like π or e. Syntax: Math.xyz (no parenthesis)
* The build-in object Math provides common mathematical functions like sin or log. Syntax: Math.xyz (with parenthesis)
* The object Number provides properties that characterize the implementation of the data type number, like MAX_VALUE or NEGATIVE_INFINITY. Syntax: Number.xyz (no parenthesis)
* The object Number provides static methods that check the relation between numbers and other data types, e.g., isInteger or parseFloat. Syntax: Number.xyz (with parenthesis)
* The object Number provides instance methods that act on concrete number values or variables, e.g., toExponential or toFixed. Syntax: value.xyz (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:
* Math.E Returns the constant e.
* Math.PI Returns the constant pi.
* Math.LN10 Returns the natural logarithm of 10.
* Math.LN2 Returns the natural logarithm of 2.
* Math.SQRT2 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 parseInt and parseFloat 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 BigInt function.
BigInt supports the arithmetic operators + - * / **, comparison operators, and most of the bitwise operators.

## Boolean

Boolean variables can contain one of two possible values, true or false. JavaScript represents false 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 true.

## 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 typeof 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.

## See also

ECMAScript definitions on data types

## Exercises

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

## References

* ↑ MDN: Null