Logical Operators
JavaScript Logical Operators
Logical operators are used to determine the logic between variables or values.
Given that x = 6 and y = 3, the table below explains the logical operators:
Oper | Name | Example | Try it |
---|---|---|---|
&& | AND | (x < 10 && y > 1) is true | Try it » |
|| | OR | (x === 5 || y === 5) is false | Try it » |
! | NOT | !(x === y) is true | Try it » |
Browser Support
Logical Operators
is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
Conditional (Ternary) Operator
The conditional operator assigns a value to a variable based on a condition.
Syntax | Example | Try it |
---|---|---|
(condition) ? x : y | (z < 18) ? x : y | Try it » |
Browser Support
The Ternary Operator
is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
The Optional Chaining Operator (?.)
The ?.
operator returns undefined
if an object is
undefined
or null
(instead of throwing an error).
Example
// Create an object:
const car = {type:"Fiat", model:"500", color:"white"};
// Ask for car name:
document.getElementById("demo").innerHTML = car?.name;
Try it Yourself »
The optional chaining operator is supported in all browsers since March 2020:
Chrome 80 | Edge 80 | Firefox 72 | Safari 13.1 | Opera 67 |
Feb 2020 | Feb 2020 | Jan 2020 | Mar 2020 | Mar 2020 |