Skip to content

Instantly share code, notes, and snippets.

Created December 4, 2016 03:40
Show Gist options
  • Save anonymous/cb455d81f2094b32fb4c9e1bc583ac88 to your computer and use it in GitHub Desktop.
Save anonymous/cb455d81f2094b32fb4c9e1bc583ac88 to your computer and use it in GitHub Desktop.
Operators Operators studies // source https://jsbin.com/pucoqu
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Operators studies">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Operators</title>
</head>
<body>
<script id="jsbin-javascript">
// -----Operators----- //
/* In Javascript, operators can perform a number of tasks. Unary operators take only one value: */
"use strict";
console.log(typeof 10); // "typeof" is a unary operator
console.log(!true); // "!" flips the following value, logging false
/* Binary operators can be placed between two values and return a new value: */
console.log(10 + 10); // the addition operator logs the sum, 20
console.log("Concat " + "me"); // it can also be used to concatenate strings
console.log(10 - 8); // the subtraction operator subtracts the values, logging 2
console.log(-(10 - 8)); // it can also be used as a unary operator, logging -2
console.log(2 * 3); // the multiplication operator multiplies, producing 6
console.log(4 / 2); // the division operator logs 2
console.log(10 % 2); // the remainder (modulo) operator logs the remainder of dividing the values
/* The assignment operator (=) assigns the value of its right operand to its left operand: */
var ex = "I'm now assigned to the variable!";
console.log(ex);
/* Comparison operators return a boolean: */
console.log(100 < 101); // true
console.log(150 > 165); // false
console.log(1000 <= 1000); // true
console.log(24 >= 23); // true
console.log(150 == '150'); // true, Equal to (==) does not compare data types
console.log(150 === '150'); // false, Strictly equal (===) compares data types, as well
console.log(150 != '150'); // false, Not equal to (!=) returns true if the values aren't equal
console.log(150 !== '150'); // true, a number does not strictly equal a string
/* Javascript can also use logical operators to reason about booleans. The aforementioned
(!) operator is one of three logical operators, including the following: */
console.log(1 < 2 && 2 < 3); /* the and (&&) operator evalutes booleans on both sides of it
and returns true only if they both evaluate to true */
console.log(5 > 3 || 3 < 1); /* the or (||) operator first evaluates the boolean on the left,
and if it evaluates to true, it returns true. If the first boolean is false, it proceeds
to the boolean on the right and returns true if it evaluates to true. This operator does
not bother to evaluate the second boolean if the first is true */
/* The final operator is the conditional operator or ternary, since it is the only ternary
operator: */
console.log(true ? 10 : 15); /* the boolean on the left of the question mark picks which value
on the right of it will be returned--true returns the value on the left of the colon, and false
returns the value on the right */
/* Operators have precedence. The || operator has the least precedence, followed by the &&
and then the comparison operators. Among the arithmetic operators, Javascript follows the
order or operations, first evaluating values within parentheses, then
multiplication/division/remainder, and finally addition/subtraction. */
/* Javascript may perform "type coercion" if the wrong type of value is applied to an operator,
changing one of the values based upon a complicated set of rules: */
console.log(10 * null); // JS decides to convert null to 0 and logs the answer, 0
/* To avoid unwanted conversions when making comparisons, use the strictly equal operator: */
console.log(false == 0); // JS may convert false to zero, resulting in an unwanted true
console.log(false === 0); // no accidental conversion!
</script>
<script id="jsbin-source-javascript" type="text/javascript">// -----Operators----- //
/* In Javascript, operators can perform a number of tasks. Unary operators take only one value: */
console.log(typeof 10); // "typeof" is a unary operator
console.log(!true); // "!" flips the following value, logging false
/* Binary operators can be placed between two values and return a new value: */
console.log(10 + 10); // the addition operator logs the sum, 20
console.log("Concat " + "me"); // it can also be used to concatenate strings
console.log(10 - 8); // the subtraction operator subtracts the values, logging 2
console.log(- (10 - 8)); // it can also be used as a unary operator, logging -2
console.log(2 * 3); // the multiplication operator multiplies, producing 6
console.log(4 / 2); // the division operator logs 2
console.log(10 % 2); // the remainder (modulo) operator logs the remainder of dividing the values
/* The assignment operator (=) assigns the value of its right operand to its left operand: */
var ex = "I'm now assigned to the variable!";
console.log(ex);
/* Comparison operators return a boolean: */
console.log(100 < 101); // true
console.log(150 > 165); // false
console.log(1000 <= 1000); // true
console.log(24 >= 23); // true
console.log(150 == '150'); // true, Equal to (==) does not compare data types
console.log(150 === '150'); // false, Strictly equal (===) compares data types, as well
console.log(150 != '150'); // false, Not equal to (!=) returns true if the values aren't equal
console.log(150 !== '150'); // true, a number does not strictly equal a string
/* Javascript can also use logical operators to reason about booleans. The aforementioned
(!) operator is one of three logical operators, including the following: */
console.log(1 < 2 && 2 < 3); /* the and (&&) operator evalutes booleans on both sides of it
and returns true only if they both evaluate to true */
console.log(5 > 3 || 3 < 1); /* the or (||) operator first evaluates the boolean on the left,
and if it evaluates to true, it returns true. If the first boolean is false, it proceeds
to the boolean on the right and returns true if it evaluates to true. This operator does
not bother to evaluate the second boolean if the first is true */
/* The final operator is the conditional operator or ternary, since it is the only ternary
operator: */
console.log(true ? 10 : 15); /* the boolean on the left of the question mark picks which value
on the right of it will be returned--true returns the value on the left of the colon, and false
returns the value on the right */
/* Operators have precedence. The || operator has the least precedence, followed by the &&
and then the comparison operators. Among the arithmetic operators, Javascript follows the
order or operations, first evaluating values within parentheses, then
multiplication/division/remainder, and finally addition/subtraction. */
/* Javascript may perform "type coercion" if the wrong type of value is applied to an operator,
changing one of the values based upon a complicated set of rules: */
console.log(10 * null); // JS decides to convert null to 0 and logs the answer, 0
/* To avoid unwanted conversions when making comparisons, use the strictly equal operator: */
console.log(false == 0); // JS may convert false to zero, resulting in an unwanted true
console.log(false === 0); // no accidental conversion!</script></body>
</html>
// -----Operators----- //
/* In Javascript, operators can perform a number of tasks. Unary operators take only one value: */
"use strict";
console.log(typeof 10); // "typeof" is a unary operator
console.log(!true); // "!" flips the following value, logging false
/* Binary operators can be placed between two values and return a new value: */
console.log(10 + 10); // the addition operator logs the sum, 20
console.log("Concat " + "me"); // it can also be used to concatenate strings
console.log(10 - 8); // the subtraction operator subtracts the values, logging 2
console.log(-(10 - 8)); // it can also be used as a unary operator, logging -2
console.log(2 * 3); // the multiplication operator multiplies, producing 6
console.log(4 / 2); // the division operator logs 2
console.log(10 % 2); // the remainder (modulo) operator logs the remainder of dividing the values
/* The assignment operator (=) assigns the value of its right operand to its left operand: */
var ex = "I'm now assigned to the variable!";
console.log(ex);
/* Comparison operators return a boolean: */
console.log(100 < 101); // true
console.log(150 > 165); // false
console.log(1000 <= 1000); // true
console.log(24 >= 23); // true
console.log(150 == '150'); // true, Equal to (==) does not compare data types
console.log(150 === '150'); // false, Strictly equal (===) compares data types, as well
console.log(150 != '150'); // false, Not equal to (!=) returns true if the values aren't equal
console.log(150 !== '150'); // true, a number does not strictly equal a string
/* Javascript can also use logical operators to reason about booleans. The aforementioned
(!) operator is one of three logical operators, including the following: */
console.log(1 < 2 && 2 < 3); /* the and (&&) operator evalutes booleans on both sides of it
and returns true only if they both evaluate to true */
console.log(5 > 3 || 3 < 1); /* the or (||) operator first evaluates the boolean on the left,
and if it evaluates to true, it returns true. If the first boolean is false, it proceeds
to the boolean on the right and returns true if it evaluates to true. This operator does
not bother to evaluate the second boolean if the first is true */
/* The final operator is the conditional operator or ternary, since it is the only ternary
operator: */
console.log(true ? 10 : 15); /* the boolean on the left of the question mark picks which value
on the right of it will be returned--true returns the value on the left of the colon, and false
returns the value on the right */
/* Operators have precedence. The || operator has the least precedence, followed by the &&
and then the comparison operators. Among the arithmetic operators, Javascript follows the
order or operations, first evaluating values within parentheses, then
multiplication/division/remainder, and finally addition/subtraction. */
/* Javascript may perform "type coercion" if the wrong type of value is applied to an operator,
changing one of the values based upon a complicated set of rules: */
console.log(10 * null); // JS decides to convert null to 0 and logs the answer, 0
/* To avoid unwanted conversions when making comparisons, use the strictly equal operator: */
console.log(false == 0); // JS may convert false to zero, resulting in an unwanted true
console.log(false === 0); // no accidental conversion!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment