JavaScript Course 2: Operators: Difference between revisions
m Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:JavaScript |
m Text replacement - "mlw-continue" to "code-continue" |
||
Line 20: | Line 20: | ||
}} | }} | ||
'''Expression''' in JavaScript is something that produces a value, for example <code>x + y</code> is an expression. | '''Expression''' in JavaScript is something that produces a value, for example <code>x + y</code> is an expression. | ||
<syntaxhighlight lang="javascript" class=" | <syntaxhighlight lang="javascript" class="code-continue"> | ||
let x = 10; | let x = 10; | ||
let y = 3; | let y = 3; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="javascript" class=" | <syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log(x ** y); // Exponetiation: 'x' to the power of 'y' | console.log(x ** y); // Exponetiation: 'x' to the power of 'y' | ||
</syntaxhighlight><syntaxhighlight lang="shell-session"> | </syntaxhighlight><syntaxhighlight lang="shell-session"> | ||
Line 46: | Line 46: | ||
}} | }} | ||
<syntaxhighlight lang="javascript" class=" | <syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log(x + y); // 13 | console.log(x + y); // 13 | ||
console.log(x - y); // 7 | console.log(x - y); // 7 | ||
Line 82: | Line 82: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="javascript" class=" | <syntaxhighlight lang="javascript" class="code-continue"> | ||
x = 10; console.log(x); // 10 | x = 10; console.log(x); // 10 | ||
x += 10; console.log(x); // 20 | x += 10; console.log(x); // 20 | ||
Line 108: | Line 108: | ||
}} | }} | ||
<syntaxhighlight lang="javascript" class=" | <syntaxhighlight lang="javascript" class="code-continue"> | ||
x = 1; | x = 1; | ||
console.log(x > 0 ); // true | console.log(x > 0 ); // true | ||
Line 123: | Line 123: | ||
*<code style="font-family: inherit;">'''=='''</code> Is equal to? (<u>lose equality</u> operator: compare <u>only the Value</u> the variable) | *<code style="font-family: inherit;">'''=='''</code> Is equal to? (<u>lose equality</u> operator: compare <u>only the Value</u> the variable) | ||
*<code style="font-family: inherit;">'''!='''</code> Is not equal to? (<u>lose not equality</u> operator: compare <u>only the Value</u> of the variable) | *<code style="font-family: inherit;">'''!='''</code> Is not equal to? (<u>lose not equality</u> operator: compare <u>only the Value</u> of the variable) | ||
<syntaxhighlight lang="javascript" class=" | <syntaxhighlight lang="javascript" class="code-continue"> | ||
let x = 1; | let x = 1; | ||
</syntaxhighlight><syntaxhighlight lang="javascript" class=" | </syntaxhighlight><syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log(x === 1); // true | console.log(x === 1); // true | ||
console.log(x !== 1); // false | console.log(x !== 1); // false | ||
Line 134: | Line 134: | ||
</syntaxhighlight><syntaxhighlight lang="javascript" class=" | </syntaxhighlight><syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log(x === '1'); // false (because '1' is a string not a number) | console.log(x === '1'); // false (because '1' is a string not a number) | ||
Line 140: | Line 140: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="javascript" class=" | <syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log(x == '1'); // true (because we compare only the values) | console.log(x == '1'); // true (because we compare only the values) | ||
Line 146: | Line 146: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="javascript" class=" | <syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log(x == true ); // true (because 'x' will be converted to a bulean before the evaluation) | console.log(x == true ); // true (because 'x' will be converted to a bulean before the evaluation) | ||
console.log(x == false); // false | console.log(x == false); // false | ||
Line 159: | Line 159: | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
let variable = (condition) ? value_if_true : value_if_false; | let variable = (condition) ? value_if_true : value_if_false; | ||
</syntaxhighlight><syntaxhighlight lang="javascript" class=" | </syntaxhighlight><syntaxhighlight lang="javascript" class="code-continue"> | ||
let poinsts = 110; | let poinsts = 110; | ||
let type = points > 100 ? 'good customer' : 'bad customer'; | let type = points > 100 ? 'good customer' : 'bad customer'; | ||
</syntaxhighlight><syntaxhighlight lang="javascript" class=" | </syntaxhighlight><syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log('The customer is: ' + type); | console.log('The customer is: ' + type); | ||
</syntaxhighlight><syntaxhighlight lang="shell-session" class=" | </syntaxhighlight><syntaxhighlight lang="shell-session" class="code-continue"> | ||
The customer is: good customer | The customer is: good customer | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 235: | Line 235: | ||
</syntaxhighlight><syntaxhighlight lang="javascript" class=" | </syntaxhighlight><syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log(currentColor); | console.log(currentColor); | ||
</syntaxhighlight><syntaxhighlight lang="shell-session"> | </syntaxhighlight><syntaxhighlight lang="shell-session"> | ||
red | red | ||
</syntaxhighlight><syntaxhighlight lang="javascript" class=" | </syntaxhighlight><syntaxhighlight lang="javascript" class="code-continue"> | ||
userColor = undefined; | userColor = undefined; | ||
console.log(currentColor); | console.log(currentColor); | ||
Line 249: | Line 249: | ||
Bitwise operators are similar to Logical operators but '''they work wit the individual bits of a number'''. Bitwise operators are rarely used in the days practice, but they have their place. | Bitwise operators are similar to Logical operators but '''they work wit the individual bits of a number'''. Bitwise operators are rarely used in the days practice, but they have their place. | ||
{{:{{FULLPAGENAME}}/BinaryNumbers}} | {{:{{FULLPAGENAME}}/BinaryNumbers}} | ||
'''<code class="noTypo">|</code>''' Bitwise <code>OR</code> (single pype/vertical line). It compares each bit of the binary numbers in the expression and if one of them is <code>1</code> it returns <code>1</code>.<syntaxhighlight lang="javascript" class=" | '''<code class="noTypo">|</code>''' Bitwise <code>OR</code> (single pype/vertical line). It compares each bit of the binary numbers in the expression and if one of them is <code>1</code> it returns <code>1</code>.<syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log(1 | 2); // 3 | console.log(1 | 2); // 3 | ||
</syntaxhighlight><syntaxhighlight lang="text" class=" | </syntaxhighlight><syntaxhighlight lang="text" class="code-continue"> | ||
1 = 00000001 | 1 = 00000001 | ||
2 = 00000010 | 2 = 00000010 | ||
3 = 00000011 (this is the result) | 3 = 00000011 (this is the result) | ||
</syntaxhighlight>'''<code class="noTypo">&</code>''' Bitwise <code>AND</code> (single ampersand). It compares each bit of the binary numbers in the expression and if one of them is <code>0</code> it returns <code>0</code>.<syntaxhighlight lang="javascript" class=" | </syntaxhighlight>'''<code class="noTypo">&</code>''' Bitwise <code>AND</code> (single ampersand). It compares each bit of the binary numbers in the expression and if one of them is <code>0</code> it returns <code>0</code>.<syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log(1 & 2); // 0 | console.log(1 & 2); // 0 | ||
</syntaxhighlight><syntaxhighlight lang="text" class=" | </syntaxhighlight><syntaxhighlight lang="text" class="code-continue"> | ||
1 = 00000001 | 1 = 00000001 | ||
2 = 00000010 | 2 = 00000010 | ||
Line 264: | Line 264: | ||
== Operators Precedence == | == Operators Precedence == | ||
<syntaxhighlight lang="javascript" class=" | <syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log(100 + 50 * 3); // 250 | console.log(100 + 50 * 3); // 250 | ||
</syntaxhighlight>As in traditional school mathematics, the multiplication is done first. Multiplication (<code>*</code>) and division (<code>/</code>) have higher precedence than addition (<code>+</code>) and subtraction (<code>-</code>). And (as in school mathematics) the precedence can be changed by using parentheses:<syntaxhighlight lang="javascript" class=" | </syntaxhighlight>As in traditional school mathematics, the multiplication is done first. Multiplication (<code>*</code>) and division (<code>/</code>) have higher precedence than addition (<code>+</code>) and subtraction (<code>-</code>). And (as in school mathematics) the precedence can be changed by using parentheses:<syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log((100 + 50) * 3); // 450 | console.log((100 + 50) * 3); // 450 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 273: | Line 273: | ||
== String operators == | == String operators == | ||
The <code>+</code> operator can also be used to add (concatenate) strings.<syntaxhighlight lang="javascript" class=" | The <code>+</code> operator can also be used to add (concatenate) strings.<syntaxhighlight lang="javascript" class="code-continue"> | ||
let text1 = "John"; | let text1 = "John"; | ||
let text2 = "Doe"; | let text2 = "Doe"; | ||
let text3 = text1 + " " + text2; | let text3 = text1 + " " + text2; | ||
</syntaxhighlight><syntaxhighlight lang="javascript" class=" | </syntaxhighlight><syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log(text3); | console.log(text3); | ||
</syntaxhighlight><syntaxhighlight lang="shell-session" class=" | </syntaxhighlight><syntaxhighlight lang="shell-session" class="code-continue"> | ||
John Doe | John Doe | ||
</syntaxhighlight> | </syntaxhighlight> | ||
The <code>+=</code> assignment operator can also be used to add (concatenate) strings:<syntaxhighlight lang="javascript" class=" | The <code>+=</code> assignment operator can also be used to add (concatenate) strings:<syntaxhighlight lang="javascript" class="code-continue"> | ||
let text1 = "What a very "; | let text1 = "What a very "; | ||
text1 += "nice day!"; | text1 += "nice day!"; | ||
</syntaxhighlight><syntaxhighlight lang="javascript" class=" | </syntaxhighlight><syntaxhighlight lang="javascript" class="code-continue"> | ||
console.log(text1); | console.log(text1); | ||
</syntaxhighlight><syntaxhighlight lang="shell-session" class=" | </syntaxhighlight><syntaxhighlight lang="shell-session" class="code-continue"> | ||
What a very nice day! | What a very nice day! | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 07:30, 26 September 2022
References
- Code with Mosh: The Ultimate JavaScript Mastery Series – Part 1
- W3School: JavaScript Operators
- W3School: JavaScript Comparison and Logical Operators
- W3School: JavaScript Arithmetic
Operators in JavaScript
The different kinds of operators in JavaScript are as follow:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Ternary (conditional) operators
- Logical operators
- Bitwise operators
Expression in JavaScript is something that produces a value, for example x + y
is an expression.
let x = 10;
let y = 3;
console.log(x ** y); // Exponetiation: 'x' to the power of 'y'
1000
Arithmetic operators
Arithmetic operators are used for performing calculations just like we performing calculations in mathematics. The Arithmetic operators are:
+
Addition operator−
Subtraction operator*
Multiplication operator/
Division operator%
Remainder of division operator (modulus)**
Exponentiation operator++
Increment operator--
Decrement operator
console.log(x + y); // 13
console.log(x - y); // 7
console.log(x * y); // 30
console.log(x / y); // 3.3333333333333335
console.log(x % y); // 1
console.log(x ** y); // 1000
console.log(++x); // 11 (the value is incremented by 1 before the expression evaluation)
console.log(x++); // 10 (the value is incremented by 1 after the expression evaluation)
console.log(--x); // 9 (the value is decremented by 1 before the expression evaluation)
console.log(x--); // 10 (the value is decremented by 1 after the expression evaluation)
Assignment operators
Assignment operators are used to assign a value to a variable. Assignment operators are:
=
Simple assignment operator+=
Addition assignment operator−=
Subtraction assignment operator*=
Multiplication assignment operator/=
Division assignment operator%=
Remainder of division assignment operator**=
Exponentiation assignment operator++
Increment operator also assign a new value to the variable--
Decrement operator also assign a new value to the variable
x++; // is equal to
x = x + 1; // is equal to
x += 1;
x = 10; console.log(x); // 10
x += 10; console.log(x); // 20
x -= 10; console.log(x); // 10
x *= 10; console.log(x); // 100
x /= 10; console.log(x); // 10
x **= 3; console.log(x); // 1000
x %= 3; console.log(x); // 1
Comparison operators
Comparison operators are used to compare the value of a variable with something else and return a Boolean true
/false
. Comparison operators are:
Relational operators
>
Is greater than?>=
Is greater than or equal to?<
Is less than?<=
Is less than or equal to?
x = 1;
console.log(x > 0 ); // true
console.log(x >= 1); // true
console.log(x < 1 ); // false
console.log(x <= 1); // true
Equality operators
===
Is equal to? (strict equality operator: compare the Value and the Type of the variable)!==
Is not equal to? (strict not equality operator: compare the Value and the Type of the variable)==
Is equal to? (lose equality operator: compare only the Value the variable)!=
Is not equal to? (lose not equality operator: compare only the Value of the variable)
let x = 1;
console.log(x === 1); // true
console.log(x !== 1); // false
console.log(x === '1'); // false (because '1' is a string not a number)
console.log(x == '1'); // true (because we compare only the values)
console.log(x == true ); // true (because 'x' will be converted to a bulean before the evaluation)
console.log(x == false); // false
console.log(x === true); // false (because 'x' is a number not a bulean)
Ternary (conditional) operators
Ternary (conditional) operators assigns a value to a variable based on some condition.
let variable = (condition) ? value_if_true : value_if_false;
let poinsts = 110;
let type = points > 100 ? 'good customer' : 'bad customer';
console.log('The customer is: ' + type);
The customer is: good customer
Logical operators
Logical operators are used to make decisions based at multiple conditions that return Boolean values – false
or true
. In JavaScript we have three types of logical operators.
&&
Logical AND
– returns TRUE if both operands are TRUE.
console.log(true && true ); // true
console.log(false && true ); // false
console.log(true && false); // false
||
Logical OR
– returns TRUE if one operands are TRUE.
console.log(true || true ); // true
console.log(false || true ); // true
console.log(true || false); // true
console.log(false || false); // false
!
Logical NOT
– gives an opposite logic.
console.log(! true ); // false
console.log(! false); // true
Note the following construction could be useful in some cases but it is not substitution of if.. then.. else
statement, and should be used carefully:
(true) && console.log('then do') || console.log('else do');
In this case (in a real world application) if the expressing 'then do'
in someway returns true
the expression 'else do'
will be executed too.
Logical operators with Non-booleans – Truthy and Falsy
Within JavaScript we can use the logical operators with non boolean values. Here is how a logical expression works with boolean values:
console.log(false || true); // true (as it is in the examples above)
The result of a logical expression is not necessarily a true
or false
that depends on the values and the operands we have.
console.log(false || 'Spas'); // "Spas"
console.log(false || '1'); // 1
The JavaScript engine tries to evaluates the expressions, looks at each operand and if the operand is not a boolean true
or false
it will tried to interpreted as what we called truthy
or falsy
. Falsy
is not exactly a boolean false
and truthy
is not a boolean true
.
The JavaScript falsy
values are:
undefined
null
0
false
''
(empty string)NaN
(not a number)
Anything is not falsy
is truthy
!
Examples with short-circuiting where the evaluations stops at some truthy
value, no matter how many other operands we have on the right side they will be ignored:
console.log(false || 1 || 2); // 1
console.log(false || 1 || 0 || 2); // 1
An example for providing default values by using this technique.
let userColor = 'red';
let defaultColor = 'blue';
let currentColor = userColor || defaultColor;
console.log(currentColor);
red
userColor = undefined;
console.log(currentColor);
blue
Bitwise operators
Bitwise operators are similar to Logical operators but they work wit the individual bits of a number. Bitwise operators are rarely used in the days practice, but they have their place.
Numbers in Decimal system | Numbers in Binary system |
---|---|
1
|
00000001
|
2
|
00000010
|
3
|
00000011
|
4
|
00000100
|
5
|
00000101
|
Numbers in Decimal system | Numbers in Binary system |
---|---|
6
|
00000110
|
7
|
00000111
|
8
|
00001000
|
9
|
00001001
|
0
|
00000000
|
- In the binary system each number is represented ot 8‑bits, that 1‑byte: wikipedia:Binary_number, https://decimaltobinary.pro/
|
Bitwise OR
(single pype/vertical line). It compares each bit of the binary numbers in the expression and if one of them is 1
it returns 1
.
console.log(1 | 2); // 3
1 = 00000001
2 = 00000010
3 = 00000011 (this is the result)
&
Bitwise AND
(single ampersand). It compares each bit of the binary numbers in the expression and if one of them is 0
it returns 0
.
console.log(1 & 2); // 0
1 = 00000001
2 = 00000010
0 = 00000000 (this is the result)
In JavaScript there are few more Bitwise operators, but in the real world these are not that common. For more details read the article: W3Schools: JavaScript Bitwise Operations. One real world example of Bitwise operators usage is Bitwise operators for permissions.
Operators Precedence
console.log(100 + 50 * 3); // 250
As in traditional school mathematics, the multiplication is done first. Multiplication (*
) and division (/
) have higher precedence than addition (+
) and subtraction (-
). And (as in school mathematics) the precedence can be changed by using parentheses:
console.log((100 + 50) * 3); // 450
More about the operators precedence could be found at the article W3School: JavaScript Arithmetic – JavaScript Operator Precedence Values.
String operators
The +
operator can also be used to add (concatenate) strings.
let text1 = "John";
let text2 = "Doe";
let text3 = text1 + " " + text2;
console.log(text3);
John Doe
The +=
assignment operator can also be used to add (concatenate) strings:
let text1 = "What a very ";
text1 += "nice day!";
console.log(text1);
What a very nice day!
If you add a number and a string, the result will be a string!
Type operators
typeof
– returns the type of a variable.instanceof
– returns true if an object is an instance of an object type.
For more information read: 1) W3School: JavaScript Type Conversion, 2) W3School: Java instanceof Keyword, 3) W3School: JavaScript typeof.