Introduction to DataTypes and Values

We'll start with an article that describes how a computer stores values and some primitive types such as numbers and strings. You can think of the "type of a value" as defining how it is stored, named, and manipulated. For example, a calculator program uses the type "number", which holds "numeric values" and supports "arithmetic operations". JavaScript has two types: "primitive" and "object". Examples of primitive types include numbers, strings, and Booleans. Object types include arrays, objects, and functions.

Here are some things to remember when using JavaScript data types:

  • Primitive types are "immutable"; their "values" cannot be changed once created;
  • Object types are "mutable"; their values can change once created; and
  • Although JavaScript supports types, it is a "dynamically typed" language, which means that you do not have to define the type of variable in a JavaScript program (but this is not a best practice).

Strings

The next basic data type is the string. Strings are used to represent text. They are written by enclosing their content in quotes.

`Down on the sea`
"Lie on the ocean"
'Float on the ocean'

You can use single quotes, double quotes, or backticks to mark strings, as long as the quotes at the start and the end of the string match.

Almost anything can be put between quotes, and JavaScript will make a string value out of it. But a few characters are more difficult. You can imagine how putting quotes between quotes might be hard. Newlines (the characters you get when you press enter) can be included without escaping only when the string is quoted with backticks (`).

To make it possible to include such characters in a string, the following notation is used: whenever a backslash (\) is found inside quoted text, it indicates that the character after it has a special meaning. This is called escaping the character. A quote that is preceded by a backslash will not end the string but be part of it. When an n character occurs after a backslash, it is interpreted as a newline. Similarly, a t after a backslash means a tab character. Take the following string:

"This is the first line\nAnd this is the second"

The actual text contained is this:

This is the first line
And this is the second

There are, of course, situations where you want a backslash in a string to be just a backslash, not a special code. If two backslashes follow each other, they will collapse together, and only one will be left in the resulting string value. This is how the string "A newline character is written like "\n"". can be expressed:

"A newline character is written like \"\\n\"."

Strings, too, have to be modeled as a series of bits to be able to exist inside the computer. The way JavaScript does this is based on the Unicode standard. This standard assigns a number to virtually every character you would ever need, including characters from Greek, Arabic, Japanese, Armenian, and so on. If we have a number for every character, a string can be described by a sequence of numbers.

And that's what JavaScript does. But there's a complication: JavaScript's representation uses 16 bits per string element, which can describe up to 216 different characters. But Unicode defines more characters than that – about twice as many, at this point. So some characters, such as many emoji, take up two "character positions" in JavaScript strings.

Strings cannot be divided, multiplied, or subtracted, but the + operator can be used on them. It does not add, but it concatenates – it glues two strings together. The following line will produce the string "concatenate":

"con" + "cat" + "e" + "nate"

String values have a number of associated functions (methods) that can be used to perform other operations on them.

Strings written with single or double quotes behave very much the same – the only difference is in which type of quote you need to escape inside of them. Backtick-quoted strings, usually called template literals, can do a few more tricks. Apart from being able to span lines, they can also embed other values.

`half of 100 is ${100 / 2}`

When you write something inside ${} in a template literal, its result will be computed, converted to a string, and included at that position. The example produces "half of 100 is 50".