Ressources:
- Précédence des opérateurs sur MDN WebDocs (ordre de priorité d’exécution des opérateurs)
Exemple d’opérateurs: delete, &&, =, +, !, *, ||, typeof
Jargon
a = 10
« a » et « 10 » sont des operands ou des arguments.-
Unary operators: un opérateur qui n’a qu’un seul argument:
12345a+++adelete obj.atypeof anew Object() -
Binary operators: un opérateur qui a 2 arguments:
12345a = 5a + ba += 5a === ba && b
Notation
Infix notation
Opérateur est déclaré ENTRE 2 operands/arguments:
1 2 3 4 5 |
a = true a + b a += 5 a || b a > b |
Prefix notation
Opérateur est déclaré AVANT l’operand/argument:
1 2 3 |
++a delete obj.a typeof a |
Postfix notation
Opérateur est déclaré APRES l’operand/argument:
1 2 |
a++ myFunction() |
Demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
let a, b; a = 1; b = 2; // Math with numbers console.log(a + b); // 3 console.log(a * b); // 2 console.log(a / b); // 0.5 console.log(a - b); // -1 a = "abc"; b = 5; // Math with non-numbers console.log(a + b); // abc5 console.log(a * b); // NaN console.log(a / b); // NaN console.log(a - b); // NaN a = "Hello"; b = "World"; // Strings concatenation console.log(a + " " + b); // Hello World // Unary plus (convertir du texte en nombres) c = "1"; <= texte console.log(+c); // 1 console.log(Number(c)); // 1 c = undefined; console.log(+c); // NaN // Unary minus c = "5"; <= texte console.log(-c); // -5 c = "abc"; console.log(-c); // NaN // ++ let d = 5; ++d; // Same as d = d + 1; console.log(d); // 6 d++; console.log(d); // 7 // Built-in "++" function FIRST increments value of the operand and SECOND returns value of the operand console.log(++d); // 8 // Built-in "++" function FIRST returns value of the operand and SECOND increments value of the operand console.log(d++); // 8 console.log(d); // 9 // -- console.log(d--); // 9 console.log(d); // 8 console.log(--d); // 7, Same as d = d console.log(d); // 7 |
Opérateurs de comparaison
<, <=, >, <=, ==, !=, ===, !==
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/* * Section 2 * * Comparison operators */ let a, b; a = 5; b = 7; c = 5; console.log(a < b); // true console.log(a > b); // false console.log(a <= c); // true console.log(a >= c); // true let myStr1, myStr2; myStr1 = "abc"; myStr2 = "bcd"; myStr3 = "Bcd"; // Sorted strings (smaller > taller) "Bcd", "abc", "bcd" console.log(mystr1 > myStr2); // false (a=1, b=2, c=3) console.log(mystr1 < myStr2); // true (b=2, c=3, a=1) console.log(mystr1 > myStr3); // true |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* * Section 2 * * Equality operators * ALWAYS USE "STRICT" EQUALITY/INEQUALITY * NEVER USE "==" and "!=" operators! */ let myString = "0"; let myNumber = 0; let myBoolean = false; // Types of the values may be different console.log(myString == myNumber); // true console.log(myNumber == myBoolean); // true console.log(myString == myBoolean); // true // TYPE and VALUE are compared console.log(myString === myNumber); // false console.log(myNumber === myBoolean); // false console.log(myString === myBoolean); // false // How you SHOULD compare variables of different types console.log(Number(myString) === myNumber); // true! Explicit conversion of string to number console.log(+myString === myNumber); // true! Explicit conversion of string to number console.log(myString != myNumber); // false console.log(myString !== myNumber); // true console.log(null === undefined); // false (different value types) console.log(null == undefined); // true (both equal "0") console.log(0 === "" === null === undefined === false); // true |
Opérateurs logiques (logical operators)
and, or, not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
/* * Logical operators */ // OR // returns first operand evaluated as TRUE console.log(true || false); // true console.log(false || true); // true (si "false", on passe à l'argument suivant) console.log("abc" || ""); // abc Boolean("a"); // true Boolean(""); // false console.log("" || "abc"); // abc (si "false", on passe à l'argument suivant) console.log("" || ""); // "" // Falsy values // All those values, after converted to boolean type, are evaluated as false. console.log("" || 0 || null || undefined || NaN || false); // false (valeur du dernier operand, car tous les operands précédents ont été évalués comme "false") let city; const defaultCity = "New York" console.log(city || defaultCity); // New York ("city" = undefined, donc on nous retourne "default"); let myCity = city || defaultCity; console.log(myCity); // New York (if "city" is Falsy => "defaultCity") city = "Los Angeles"; myCity = city || defaultCity; console.log(myCity); // Los Angeles ("city" now contains an non empty string, so it returns true. Evaluation is stopped when first true value is found) let myOtherCity = city || console.log("Fill in city please") || defaultCity; console.log("myOtherCity " + myOtherCity); // Los Angeles // AND operator // returns first operand evaluated as FALSE console.log(true && false); // false console.log(false && true); // false // And returns value of the first falsy operand console.log("abc" && 10 && false && "" && "abcd"); // false console.log("abc" && 10 && NaN && "" && "abcd"); // NaN (get value of first Falsy operand) console.log("abc" && 10 && "" && "abcd"); // "" // All operands are "truthy" console.log("abc" && 10 && true && 123 && "string"); // string (when all operands are evaluated to true, value of the last operand is returned) console.log("abc" && 10 && true && 123 && "Hello World"); // Hello World Boolean("Hello World"); // true // NOT operator console.log(!"abc"); // false console.log(!""); // true (empty string is falsy value) console.log(!0); // true let myVariable; myVariable = undefined; console.log(!myVariable); // true myVariable = "Frank"; console.log(!myVariable); // false // Quick truthy/falsy check myVariable = null; console.log(!!myVariable); // false (because "null" is falsy value) myVariable = 10; console.log(!!myVariable); // true (because "10" is truthy value) |
Cas pratiques
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// Task 1 let myVariable1 = "2"; let myVariable2 = "4"; console.log(Number(myVariable1) <= Number(myVariable2)); console.log(+myVariable1 <= +myVariable2); // Task 2 // Afficher ce qu'il reste de la division 10/3 let myNumber1 = 10; let myNumber2 = 3; console.log(myNumber1 % myNumber2); // 1 (10 = 3 + 3 + 3 + 1) console.log(100 % 30 % 3); // 1 // Task 3 console.log(3 || true && null || false); // 3 // STEP 1: true && null -> null // STEP 2: 3 || null -> 3 // RESULT: 3 // Task 4 let a = 10; // a = a + 1; a += 1; console.log(a); // 11 // a = a * 2; a *= 2; console.log(a); // 22 // a = a - 5; a -= 5; console.log(a); // 17 // a = a / 2; a /= 2; console.log(a); // 8.5 |