bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/JavaScript Foundations
JavaScript•JavaScript Foundations

JavaScript Logical Operators

Logical Operators

Logical operators are used to combine boolean expressions .

Logical operators can be used to modify the results of comparisons.

Typically, you will use a comparison operator to check a condition, and a logical operator to combine conditions into more complex logic.

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:

OperNameExample
&&AND(x < 10 && y > 1) is true
OR(x === 5y === 5) is false
!NOT!(x === y) is true

JavaScript Logical AND

The && operator returns true if both expressions are true , otherwise false :

Example

let x = 6;
let y = 3;
let z = (x < 10 && y > 1)

JavaScript Logical OR

The || operator returns true if one or both expressions are true , otherwise false :

Example

let x = 6;
let y = -3;
let z = (x > 0 || y > 0)

JavaScript Logical NOT

The NOT operator (!) returns true for false expressions and false for true expressions .

Example

let x = (5 == 8);
let y = !(5 == 8)

The Nullish Coalescing Operator (??)

The ?? operator returns the right operand when the left operand is nullish ( null or undefined ), otherwise it returns the left operand.

Example

let name = null;
let text = "missing";
let result = name ?? text;

When programming, a lot of values can be falsey (like 0, empty strings, false, undefined, null, NaN).

However, sometimes you want to check if a variable is nullish (either undefined or null ), like when it is okay for a variable to be an empty string, or a false value. Then you can use the nullish coalescing operator .

Browser Support

?? is an ES2020 feature.

ES2020 is fully supported in all modern browsers since September 2020 :

Chrome 85Edge 85Firefox 79Safari 14Opera 71
Aug 2020Aug 2020Mar 2020Sep 2020Sep 2020

See Also

JavaScript Comparisons .

Previous

JavaScript Const

Next

JavaScript Control Flow

This chapter

Overview
51

Lessons

194m

Read time

1. JavaScript Tutorial2. JavaScript Syntax3. JavaScript Operators4. JavaScript if5. JavaScript Loops6. JavaScript Functions7. JavaScript Scope8. JavaScript Data Types9. JavaScript Style Guide10. JavaScript Introduction11. JavaScript Statements12. JavaScript Arithmetic13. JavaScript else14. JavaScript For Loop15. JavaScript Functions16. JavaScript Code Blocks17. JavaScript Primitives18. JavaScript Best Practices19. JavaScript Where To20. JavaScript Comments21. JavaScript Assignment22. The JavaScript Ternary Operator23. JavaScript While Loops24. Invoking JavaScript Functions25. JavaScript Hoisting26. JavaScript Built-In Objects27. JavaScript Common Mistakes28. JavaScript Output29. JavaScript Variables30. JavaScript Comparison31. JavaScript Switch Statement32. JavaScript Break33. JavaScript Function Parameters34. JavaScript Use Strict35. JavaScript typeof36. JavaScript Performance37. JavaScript Let38. JavaScript Conditionals39. JavaScript Booleans40. JavaScript Continue41. JavaScript Function Return42. JavaScript toString()43. JavaScript Const44. JavaScript Logical Operators45. JavaScript Control Flow46. JavaScript Function Arguments47. JavaScript Type Conversion48. JavaScript Datatypes49. JavaScript Function Expressions50. JavaScript Destructuring51. JavaScript Arrow Functions

On this page

Logical OperatorsJavaScript Logical OperatorsJavaScript Logical ANDJavaScript Logical ORJavaScript Logical NOTThe Nullish Coalescing Operator (??)Browser Support