JavaScript – Basic Operators

Comparison operator > / <

If we write 2 < 6 , we thus ask the question, is it true or not? The answer can be in the form of boolean values – True / False.

true > false

Equality operator (soft equals) ==

We ask, 6 == 6 (six equals six), the answer will be true

We cannot compare variables of different types, that is, we cannot compare a number and a string. If we write 6 == “6” then we are asking if the number six is equal to the string six. In this case, we will also get true, because the string will be converted to a number.

Hard equality operator (strict equality) ===

We ask, 6 === 6 (six equals six), the answer will be true

We ask, 6 === “6” (six is equal to line six), the answer will be false

If the types are different, then the answer will be false. If the types are the same, then they are compared in the usual way.

Inequality operator (soft) !=

6 != 5

true (answer)

Strict inequality operator (look at the type of variables) !==

6 !== “6”

false (answer)

Logical operators in order of precedence (important – less important):

  • all comparison operators: < , <= , > , >=
  • ! (not) – returns the opposite: !true is false
  • && (and) – for this operator to be positive, the interpreter first casts both parts to a boolean type (number 0 = false, empty string = false, rest = true). If both sides are true, then the response is true. But it returns in the answer not a Boolean number, but the number corresponding to it. For example: 4 && 5 in the answer we get 5. Why 5? Because both are true, so what should be returned as a response? What is closer (first checked 4, then 5 -> 5 is closer). If the first one was false, then it will return. Example: 0 && 8 – the answer is 0.
  • || (or) – the left one is checked first, if it is true, then it will be returned. If the first one was false and the second one was true, then the second one will be returned. If both are false, then the last one will be returned.

A semicolon is used to mark the end of a statement; but this is not necessary, because the interpreter himself affixes them for the author at his own discretion, if these signs are not affixed.

JavaScript ignores all spaces and tabs, it doesn’t care about FIG. You can format the code in any way.

This entry was posted in Javascript (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.