Javascript Operators Flashcards
Assign values to variables and add them together:
x = 5, y = 2, calculate z = x + y, and display z:
The assignment operator (=) assigns a value to a variable.
The addition operator (+) adds numbers:
The multiplication operator (*) multiplies numbers.
script> var x = 5; var y = 2; var z = x + y; document.getElementById("demo").innerHTML = z; /script>
var x = 10;
var z = x + y;
var z = x * y;
JavaScript Arithmetic Operators
Arithmetic operators are used to perform arithmetic on numbers:
Addition Subtraction Multiplication Exponentiation Division Modulus (Division Remainder) Increment Decrement
\+ Addition - Subtraction * Multiplication ** Exponentiation (ES2016) / Division % Modulus (Division Remainder) \++ Increment -- Decrement
JavaScript Assignment Operators
Assignment operators assign values to JavaScript variables.
The addition assignment operator (+=) adds a value to a variable.
script> var x = 10; x += 5; document.getElementById("demo").innerHTML = x; /script>
JavaScript String Operators
The + operator can also be used to add (concatenate) strings.
The += assignment operator can also be used to add (concatenate) strings:
When used on strings, the + operator is called the concatenation operator.
Adding Strings and Numbers
Adding two numbers, will return the sum, but adding a number and a string will return a string:
script> var txt1 = "John"; var txt2 = "Doe"; document.getElementById("demo").innerHTML = txt1 + " " + txt2; /script>
var txt1 = “What a very “;
txt1 += “nice day”;
What a very nice day
Adding Strings and Numbers
Adding two numbers, will return the sum, but adding a number and a string will return a string:
The result of x, y, and z will be:
If you add a number and a string, the result will be a string!
script> var x = 5 + 5; var y = "5" + 5; var z = "Hello" + 5; document.getElementById("demo").innerHTML = x + "br>" + y + "br>" + z; /script>
10
55
Hello5
JavaScript Comparison Operators
equal to equal value and equal type not equal not equal value or not equal type greater than less than greater than or equal to less than or equal to ternary operator
== equal to === equal value and equal type != not equal !== not equal value or not equal type > greater than < less than >= greater than or equal to <= less than or equal to ? ternary operator
JavaScript Logical Operators
logical and
logical or
logical not
&& logical and
|| logical or
! logical not
JavaScript Type Operators
typeof
instanceof
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an object type