Last active
November 16, 2020 11:44
-
-
Save robbypelssers/9994915 to your computer and use it in GitHub Desktop.
Javascript: Operators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Try this code out at http://repl.it/languages/JavaScript | |
| /********************************************************************************************** | |
| Operators listed by precedence: | |
| Operator Operation A N | |
| -------------------------------------------------------------- | |
| ++ Pre-or post increment R 1 | |
| -- Pre-or post decrement R 1 | |
| - Negate number R 1 | |
| + Convert to number R 1 | |
| ~ Invert bits R 1 | |
| ! Invert boolean value R 1 | |
| delete Remove a property R 1 | |
| typeof Determine type of operand R 1 | |
| void Return undefined value R 1 | |
| *, /, % Multiply, divide, remainder L 2 | |
| +, - Add, subtract L 2 | |
| + Concatenate strings L 2 | |
| << shift left L 2 | |
| >> shift right with sign extension L 2 | |
| >>> shift right with zero extension L 2 | |
| <, <=, >, >= Compare in numeric order L 2 | |
| <, <=, >, >= Compare in alphabetic order L 2 | |
| instanceof Test object class L 2 | |
| in Test whether property exists L 2 | |
| == Test for equality L 2 | |
| != Test for inequality L 2 | |
| === Test for strict equality L 2 | |
| !== Test for strict inequality L 2 | |
| & Compute bitwise AND L 2 | |
| ^ Compute bitwise XOR L 2 | |
| | Compute bitwise OR L 2 | |
| && Compute logical AND L 2 | |
| || Compute logical OR L 2 | |
| ?: Choose 2nd or 3rd operand R 3 | |
| = Assign to a variable or property R 2 | |
| *=, /=, %=, += Operate and assign R 2 | |
| -=, &=, ^=, |= | |
| <<=, >>=, >>>= | |
| , Discard 1st operand, return 2nd L 2 | |
| /**********************************************************************************************/ | |
| /********************************************************************************************** | |
| All Javascript values are either truthy or falsy. The falsy values are | |
| false, null, undefined, 0, -0, NaN,"". All other values are truthy values. So any value can | |
| be used in a boolean expression. | |
| /**********************************************************************************************/ | |
| function isTruthyValue(value) { | |
| var isTruthy = " is " + (value ? "" : " NOT ") + " a truthy value"; | |
| console.log(value + isTruthy); | |
| } | |
| var v1 = "test"; | |
| var v2 = ""; | |
| var v3 = null; | |
| var v4 = Number("test"); | |
| var v5; //undefined | |
| var v6 = 0; | |
| var v7 = false; | |
| var v8 = [1,2]; | |
| var v9 = {}; //empty object | |
| var v10 = true; | |
| var values = [v1,v2,v3,v4,v5,v6,v7,v8,v9,v10]; | |
| values.forEach(isTruthyValue); | |
| /** | |
| test is a truthy value | |
| is NOT a truthy value | |
| null is NOT a truthy value | |
| NaN is NOT a truthy value | |
| undefined is NOT a truthy value | |
| 0 is NOT a truthy value | |
| false is NOT a truthy value | |
| 1,2 is a truthy value | |
| [object Object] is a truthy value | |
| true is a truthy value | |
| **/ | |
| /********************************************************************************************** | |
| Unary - and + operators convert their operands to a number (or NaN) | |
| /**********************************************************************************************/ | |
| var x = +true; | |
| var y = +null; | |
| var iphone = {brand: "apple", valueOf: function() { return 500}}; | |
| console.log(x); //prints 1 | |
| console.log(y); //prints 0 | |
| console.log(-iphone); //prints -500 | |
| /********************************************************************************************** | |
| The IN operator expects a left-side operand that is or can be converted to a string. | |
| It expects a right-side operand that is an object. It evaluates to true if the left-side | |
| value is the name of a property of the right-side object. | |
| /**********************************************************************************************/ | |
| var point = {x:10, y: 20}; | |
| console.log("x" in point); //true | |
| console.log("toString" in point); //true | |
| var numbers = [5,6,7]; | |
| console.log(1 in numbers); //true | |
| console.log(5 in numbers); //false | |
| delete numbers[2]; | |
| console.log(2 in numbers); //false | |
| /********************************************************************************************** | |
| The logical AND operator: | |
| (1) If used with boolean operands, it returns true if both operands are true | |
| (2) If used with truthy and falsy values: | |
| left operand is falsy => return value of left operand | |
| left operand is truthy => return value of right operand | |
| /**********************************************************************************************/ | |
| var o = { x:1}; | |
| var p = null; | |
| var t1 = o && o.x; | |
| var t2 = o && o.y; | |
| var t3 = p && p.x; | |
| console.log(t1); //prints 1 | |
| console.log(t2); //prints undefined | |
| console.log(t3); //prints null | |
| console.log(5 > 4 && true); //prints true | |
| /********************************************************************************************** | |
| The logical OR operator: | |
| (1) If used with boolean operands, it returns true if either the left or right operand are true | |
| (2) If used with truthy and falsy values: | |
| left operand is falsy => return value of right operand | |
| left operand is truthy => return value of left operand | |
| /**********************************************************************************************/ | |
| var preferences = {max_width: 50}; | |
| function setScreenSize(max_width, preferences) { | |
| var max = max_width || preferences.max_width; | |
| console.log(max); | |
| } | |
| setScreenSize(20, preferences); //prints 20 | |
| setScreenSize(null, preferences); //prints 50 | |
| /********************************************************************************************** | |
| The logical NOT operator is a unary operator which is placed before a single operand. | |
| It inverts the boolean value of its operand. If the operand x is truthy, !x evaluates to false. | |
| If x is falsy, !x evaluates to true. | |
| /**********************************************************************************************/ | |
| var x = 3; | |
| var y = null; | |
| console.log(!x); //prints false | |
| console.log(!y); //prints true | |
| /********************************************************************************************** | |
| The IN operator expects a left-side operand that is or can be converted to a string. It expects | |
| a right-side operand that is an object. It evaluates to true if the left-side value is the | |
| name of a property of the right-side object | |
| /**********************************************************************************************/ | |
| var point = {x:10, y: 20}; | |
| console.log("x" in point); //true | |
| console.log("toString" in point); //true | |
| var numbers = [5,6,7]; | |
| console.log(1 in numbers); //true | |
| console.log(5 in numbers); //false | |
| delete numbers[2]; | |
| console.log(2 in numbers); //false | |
| /********************************************************************************************** | |
| The INSTANCEOF operator expects a left-side operand that is an object and a right-side | |
| operand that identifies a class. The operator evaluates to true if the left-side object is | |
| an instance of the right-side class. | |
| Attention: this only works if you create an instance using the "new" keyword | |
| except for a function declaration where this is not needed. | |
| /**********************************************************************************************/ | |
| function greet() { | |
| console.log("hello"); | |
| } | |
| console.log({x:10, y:20} instanceof Object); //true | |
| console.log(5 instanceof Number); //false | |
| console.log(new Number(3) instanceof Number); //true | |
| console.log(false instanceof Boolean); //false | |
| console.log(Boolean(point) instanceof Boolean); //false | |
| console.log(new Boolean(false) instanceof Boolean); //true | |
| console.log(greet instanceof Function); //true | |
| /********************************************************************************************** | |
| The CONDITONIAL operator (or ternary operator) has 3 operands and following syntax | |
| operand1 ? operand2 : operand3 | |
| If operand1 is truthy, evaluate and return operand2, else evaluate and return operand3 | |
| /**********************************************************************************************/ | |
| var age = 17; | |
| var permissionToDrink = age >= 18 ? true : false; | |
| console.log("permission to drink is " + permissionToDrink); //permission to drink is false | |
| /********************************************************************************************** | |
| The TYPEOF operator is a unary operator that is placed before its single operand, which | |
| can be of any value. Its value is a string that specifies the type of the operand. | |
| undefined -> "undefined" | |
| null -> "object" | |
| true or false -> "boolean" | |
| any number or NaN -> "number" | |
| any string -> "string" | |
| any function -> "function" | |
| any non function native object -> "object" | |
| any host object -> an implementation-defined string | |
| /**********************************************************************************************/ | |
| function sum(a,b) { | |
| return a + b; | |
| } | |
| console.log(typeof 5); //number | |
| console.log(typeof "word"); //string | |
| console.log(typeof (5 > 4)); //boolean | |
| console.log(typeof null); //object | |
| console.log(typeof {x:1}); //object | |
| console.log(typeof sum); //function | |
| /********************************************************************************************** | |
| The DELETE operator is a unary operator that attempts to delete the object property or | |
| array element specified as its operand. | |
| Remark: when using delete on an array, the element is removed, but the length remains the same | |
| /**********************************************************************************************/ | |
| var point = {x: 10, y:20}; | |
| delete point.x; | |
| for (prop in point) { | |
| console.log(prop + "=" + point[prop]); | |
| } | |
| //prints only y=20 as we delete the property x; | |
| var numbers = [1,2,3,4]; | |
| console.log(numbers.length); //prints 4 | |
| delete numbers[2]; //delete the third element | |
| console.log(numbers); //prints [1,2,4] | |
| console.log(numbers.length); //prints 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment