Data types

JavaScript has three basic data types that are the building blocks of the language. These are:

  • Strings

  • Numbers

  • Boolean

Strings

Strings are any grouping of characters that are surrounded by single (' ... ') or double (" ... ") quotes. A string must start and end with the same kind of quotes. E.g. 'This is not a valid string" is not a valid string. "This is a valid string though" on the other hand is a valid string.

Sometimes you need to use the ' character inside the string, e.g. if you need to write "she's a strong girl". Since the only quote type that can end a string started with a double quote, you can use as many single quotes inside the string as you like, and vice versa.

Sometimes you want to combine two strings, this can be achieved using the operator + between the two strings. This operation is called concatenation. It's most useful together with a variable, as you'll see later.

"This is a " + "string"

>> This is a string

Note the space at the end of the first string. Using the + operator will concatenate the strings as they are, not adding any characters between the two strings.

Numbers

Numbers in JavaScript are all numeric values, both integers and decimals. They are written e.g. 42, without any quotes. Decimal numbers are written with a punctuation mark (.), e.g. 42.4.

You can also write very big or small numbers, using the scientific notation by adding an "e" (exponent), followed by the exponent of the number. E.g. 2.998e8, which is equal to 2.988 * 10^8 = 299 800 000.

There are three special numbers in JavaScript that don't behave as numbers. They are:

  • Infinity

  • -Infinity

  • NaN

Infinity and -Infinity (minus infinity) represents the positive and negative infinities. Infinity + 1 is still infinity, and so on. Using Infinity isn't mathematically solid, and will quickly lead to NaN.

NaN stands for "not a number", even though it has the type of value. You'll get this result when you try to calculate 0 / 0 (zero divided by zero), or any other numeric operation that don't yield a meaningful result.

JavaScript uses 64 bits to store numbers in the memory. Some of this bits goes to telling if the number is negative or positive, and the location of the decimal point. If you try to get a number that's larger than JavaScript's maximum number, it will overflow. This is seldom a problem though, as the largest number is around 9 quadrillion (15 zeroes).

You can do arithmetic with numbers. The operators are:

    • (addition)

    • (subtraction)

  • * (multiplication)

  • / (division)

  • % (modulo)

The arithmetic rules in JavaScript follows normal rules, i.e. that multiplication and division is done before addition and subtraction, and that the calculations are done from left to right in cases where the operations have the same precedence.

100 + 4 * 11

>> 144

Note that the addition operator is the same as the concatenation operator (+). If one of the two values on either side of this operator is a string, the result will be a concatenated string of the two values. This is called automatic type conversion, and is used in JavaScript because you don't declare the data types (this means that JavaScript is classified as a untyped/weakly typed language).

You can change the order of calculation by wrapping expressions in parentheses.

(100 + 4) * 11

>> 1144

You might not recognise the modulo (%) operator. This finds the remainder after a division.

7%5

>> 2

The remainder's precedence is the same as that of multiplication and division.

Boolean

Boolean values can be on of the values. true or false. This can represent answers like "yes" and "no", or "on" and "off". These are useful when you want to see if some condition is fulfilled or not, e.g. if a user's age is above a certain level. They are written with all lower case, and no quotes.

One way to produce a boolean value is using logical operators.

4 > 3

>> true

The logical operator ">" means "is greater than". You'll learn more about these operators soon.

3 > 4

>> false

Last updated