Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="iii" content="Operators">
- <title>Operators</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- /*
- * iii. OPERATORS
- *
- * JavaScript operators allow us to perform arithmetic, logical, and
- * various other operations on one or more values.
- */
- /*
- * a. Assignment
- */
- // The assignment operator (=) assigns a value to a variable.
- "use strict";
- var x = 12;
- console.log(x); //prints => 12
- // Shorthand variations of this operator include +=, -=, *=, /=, and %=.
- x += y; // same as x = x + y
- x -= y; // same as x = x - y
- x *= y; // same as x = x * y
- x /= y; // same as x = x / y
- x %= y; // same as x = x % y
- /*
- * b. Arithmetic
- */
- /*
- * Arithmetic operators are used to perform arithmetic using numbers
- * or the variables that store numbers:
- */
- var x = 10,
- y = 2;
- console.log(x + y); //prints 12
- console.log(x - y); //prints 8
- console.log(x * y); //prints 20
- console.log(x / y); //prints 5
- console.log(x % y); //prints 0
- /*
- *NOTE: Modulo (%), or the remainder operator, is used to show the
- * remainder from dividing two numbers:
- */
- console.log(10 % 3); //prints 1
- /*
- * There is also an increment (++) and a decrement (--) operator, which
- * increase or decrease a value by 1:
- */
- console.log(++x); //prints 11
- console.log(--y); //prints 1
- /*
- * Notice that x++ is the same as x = x + 1, or x += 1.
- * x-- is the same as x = x - 1, or x -= 1.
- */
- // The addition operator (+) can also be used to join, or concatenate, strings:
- var greeting = "Hello, my name is ";
- var myName = "Connor";
- console.log(greeting + myName); //prints "Hello, my name is Connor"
- /*
- * c. Comparison
- */
- /*
- * Comparison operators are used to test for true or false. These operators
- * include the greater than(>) and less than(<) operators,
- * the equals operators(==, ===), and the bang operator(!).
- */
- // Greater than and Less than:
- var x = 10,
- y = 2;
- console.log(x > 2); //prints true
- console.log(x < 2); //prints false
- // Greater than or equal to(>=) and Less than or equal to(<=):
- var x = 10,
- y = 10;
- console.log(x >= y); //prints true
- console.log(x <= y); //prints true
- /*
- * a triple equals sign (===) returns true if two variables or
- * values are the same value AND type:
- */
- var x = 10,
- y = 10;
- console.log(x === y); //prints true
- y = 5;
- console.log(x === y); //prints false
- y = "ten";
- console.log(x === y); //prints false
- /*
- * a double equals sign (==) returns true if two variables or values are
- * the same value. JavaScript will try to convert types to match other types,
- * but it can be finicky:
- */
- console.log(10 == "10"); //prints true
- console.log(10 == "ten"); //prints false
- /*
- * The bang (!) operator is used to negate the equals sign:
- */
- console.log(5 !== 5); //prints false
- console.log(5 !== 8); //prints true
- /*
- * d. Logical
- */
- /*
- * Logical operators determine the logical relationships between two
- * or more variables or values. These include and(&&), or(||), and not(!).
- */
- // With and(&&), both conditions must be true for the Boolean value to be true:
- console.log(3 < 10 && 12 > 9); //prints true
- console.log(3 !== 5 && 2 < 1); //prints false
- // With or(||), either condition can be true:
- console.log(3 > 4 || 1 === 1); //prints true
- console.log(1 === 1 || 1 !== 1); // prints true
- // Not(!) simply negates a statement:
- console.log(!5); //prints false
- console.log(!(3 < 1)); //prints true
- /*
- * e. Binary
- */
- /*
- * Binary operators are simply the operators that require two values:
- * one on each side of the operator. These include all of the assignment,
- * arithmetic, and logical operators that we have covered so far, in
- * addition to the instanceof operator, which is outlined below.
- */
- // instanceof lets us know if a given value is an instance of a certain type:
- var theGang = ["Dennis", "Dee", "Charlie", "Mac", "Frank"];
- console.log(theGang instanceof Array); //prints true
- console.log(theGang instanceof String); //prints false
- console.log(theGang instanceof Number); //prints false
- /*
- * f. Ternary
- */
- /*
- * There is one ternary operator in JavaScript, sometimes referred to as
- * the conditional operator. It gives a condition, and two expressions;
- * if the condition is true, it returns the value of the first expression,
- * if the condition is false, it returns the value of the second. Below
- * is the syntax and examples:
- */
- // condition ? expression1 : expression2
- var x = 5,
- y = 10;
- console.log(10 >= 8 ? x + y : x - y); //prints 15
- console.log(x >= y ? x + y : x - y); //prints -5
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">/*
- * iii. OPERATORS
- *
- * JavaScript operators allow us to perform arithmetic, logical, and
- * various other operations on one or more values.
- */
- /*
- * a. Assignment
- */
- // The assignment operator (=) assigns a value to a variable.
- var x = 12;
- console.log(x); //prints => 12
- // Shorthand variations of this operator include +=, -=, *=, /=, and %=.
- x += y; // same as x = x + y
- x -= y; // same as x = x - y
- x *= y; // same as x = x * y
- x /= y; // same as x = x / y
- x %= y; // same as x = x % y
- /*
- * b. Arithmetic
- */
- /*
- * Arithmetic operators are used to perform arithmetic using numbers
- * or the variables that store numbers:
- */
- var x = 10, y = 2;
- console.log(x + y); //prints 12
- console.log(x - y); //prints 8
- console.log(x * y); //prints 20
- console.log(x / y); //prints 5
- console.log(x % y); //prints 0
- /*
- *NOTE: Modulo (%), or the remainder operator, is used to show the
- * remainder from dividing two numbers:
- */
- console.log(10 % 3); //prints 1
- /*
- * There is also an increment (++) and a decrement (--) operator, which
- * increase or decrease a value by 1:
- */
- console.log(++x); //prints 11
- console.log(--y); //prints 1
- /*
- * Notice that x++ is the same as x = x + 1, or x += 1.
- * x-- is the same as x = x - 1, or x -= 1.
- */
- // The addition operator (+) can also be used to join, or concatenate, strings:
- var greeting = "Hello, my name is ";
- var myName = "Connor";
- console.log(greeting + myName); //prints "Hello, my name is Connor"
- /*
- * c. Comparison
- */
- /*
- * Comparison operators are used to test for true or false. These operators
- * include the greater than(>) and less than(<) operators,
- * the equals operators(==, ===), and the bang operator(!).
- */
- // Greater than and Less than:
- var x = 10, y = 2;
- console.log(x > 2); //prints true
- console.log(x < 2); //prints false
- // Greater than or equal to(>=) and Less than or equal to(<=):
- var x = 10, y = 10;
- console.log(x >= y); //prints true
- console.log(x <= y); //prints true
- /*
- * a triple equals sign (===) returns true if two variables or
- * values are the same value AND type:
- */
- var x = 10, y = 10;
- console.log(x === y); //prints true
- y = 5;
- console.log(x === y); //prints false
- y = "ten";
- console.log(x === y); //prints false
- /*
- * a double equals sign (==) returns true if two variables or values are
- * the same value. JavaScript will try to convert types to match other types,
- * but it can be finicky:
- */
- console.log(10 == "10"); //prints true
- console.log(10 == "ten"); //prints false
- /*
- * The bang (!) operator is used to negate the equals sign:
- */
- console.log(5 !== 5); //prints false
- console.log(5 !== 8); //prints true
- /*
- * d. Logical
- */
- /*
- * Logical operators determine the logical relationships between two
- * or more variables or values. These include and(&&), or(||), and not(!).
- */
- // With and(&&), both conditions must be true for the Boolean value to be true:
- console.log(3 < 10 && 12 > 9); //prints true
- console.log(3 !== 5 && 2 < 1); //prints false
- // With or(||), either condition can be true:
- console.log(3 > 4 || 1 === 1); //prints true
- console.log(1 === 1 || 1 !== 1); // prints true
- // Not(!) simply negates a statement:
- console.log(!5); //prints false
- console.log(!(3 < 1)); //prints true
- /*
- * e. Binary
- */
- /*
- * Binary operators are simply the operators that require two values:
- * one on each side of the operator. These include all of the assignment,
- * arithmetic, and logical operators that we have covered so far, in
- * addition to the instanceof operator, which is outlined below.
- */
- // instanceof lets us know if a given value is an instance of a certain type:
- var theGang = ["Dennis", "Dee", "Charlie", "Mac", "Frank"];
- console.log(theGang instanceof Array); //prints true
- console.log(theGang instanceof String); //prints false
- console.log(theGang instanceof Number); //prints false
- /*
- * f. Ternary
- */
- /*
- * There is one ternary operator in JavaScript, sometimes referred to as
- * the conditional operator. It gives a condition, and two expressions;
- * if the condition is true, it returns the value of the first expression,
- * if the condition is false, it returns the value of the second. Below
- * is the syntax and examples:
- */
- // condition ? expression1 : expression2
- var x = 5, y = 10;
- console.log(10 >= 8 ? x + y : x - y); //prints 15
- console.log(x >= y ? x + y : x - y); //prints -5
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement