Mind Diary

Values, Types, and Operators

Values:

Imagine a sea of bits–an ocean of them. To be able to work with such quantities of bits without getting lost, we must separate them into chunks that represent pieces of information.

In a JavaScript environment, those chunks are called values.
Values play different roles, and every value has a type that determines its role.
Some values are “numbers”, some values are “pieces of text”, some values are “functions”, and so on.

To create a value, you must merely invoke its name. Every value has to be stored somewhere.
As soon as you no longer use a value, it will dissipate.

This page introduces the elements of JavaScript programs, i.e. the simple value types and the operators that can act on such values.

Numbers:

Values of the number type are “numeric” values.

They are written as follows:

  • 13 (whole number, also called integer).
  • 9.81 (fractional numbers written by using a dot).

calculations with fractional numbers are generally not precise. The important thing is to be aware of it and treat fractional digital numbers as “approximations”, not as precise values.

Arithmetic:

The main thing to do with numbers is arithmetic.
Arithmetic operations such as addition or multiplication take two number values and produce a new number from them.

Here is what they look like in JavaScript: 100 + 4 ∗ 11 The + and * symbols are called operators.

Putting an operator between two values will apply it to those values and produce a new value.

In the above–mentioned the “multiplication” is done before the “adding”, i.e. the multiplication happens first.

But as in mathematics, we can change this by wrapping the addition in brackets: (100 + 4) ∗ 11

For “subtraction”, there is the − operator, and “division” can be done with the / operator.

When operators appear together without brackets, the order in which they are applied is determined by the precedence of the operators.

Multiplication comes before addition. The / operator has the same precedence as ∗. Likewise for + and −.

When multiple operators with the “same precedence” appear next to each other, as in 1 − 2 + 1, they are applied left to right: (1 − 2)+ 1.

These rules of precedence are not something you should worry about. When in doubt, just add brackets.

There is one more arithmetic operator: The % symbol is used to represent the remainder operation.

The remainder operator is often referred to as modulo.

X % Y is the remainder of “dividing” X by Y.
For example, 314 % 100 produces 14 and 144 % 12 gives 0

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

Strings:

The next basic data type is the string.
Strings are used to represent text. They are written by enclosing their content in quotes. “Lie on the ocean”
‘Float on the ocean’

We 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.

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.

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

If we want a backslash in a string to be just a backslash, not a special code, we write two backslashes that 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 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"

Unary Operators:

Some operators are written as words, such as the typeof operator, which produces a string value naming the type of the value we give it. console.log(typeof 4.5)
// → number
console.log(typeof "x")
// → string

While other operators operate on two values, the typeof operator takes only one.

Operators that use two values are called binary operators, while those that take one are called unary operators.

The minus operator () can be used both as a binary operator and as a unary operator. console.log(− (10 − 2) )
// → −8

Boolean Values:

JavaScript has a value that distinguishes between only two possibilities — the Boolean type, which has just two values, true and false.

Comparison:

Here is one way to produce Boolean values: console.log(3 > 2) // → true console.log(3 < 2) // → false