Short Circuiting OR (||)

Understanding how the OR operator works in JavaScript

The OR operator (||) evaluates operands from left to right and returns the first truthy value it encounters. If all values are falsy, it returns the last value.

In this mini project, we will demonstrate how short-circuiting works with the OR operator.

Check the code in index.js to see examples of short-circuiting with the OR operator.

Here are some examples:

			
// Example code demonstrating short-circuiting with OR operator
const jobHunter = {
    name: 'Tom Chant',
    jobSearchArea: 'Europe',
}

const workLocation = jobHunter.jobSearchArea || 'Worldwide'
console.log(`${jobHunter.name}'s work location is ${workLocation}`)
// Output: Tom Chant's work location is Europe
			
		

References:

Open the console to see the output of the code.


View Challenge