• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

The Coding Couple

Pair programming is a lifetime commitment.

  • Home
  • Categories
    • Arduino
    • JavaScript
    • Python
    • Raspberry Pi
  • About Us
    • Privacy Policy
  • Contact

JavaScript’s Null Coalescing Operator | Today I Learned

June 22, 2019 by Ashley

Scroll below for an important language update to this blog post.

I was fixing a bug in a portion of code when I came across a getter structured very similar to the lines below:

public get name(): string {
    return this._person && this._person.name || "(No name)";
}

At first glance, I was confused thinking why would you return a Boolean here? Shouldn’t this be a ternary statement? However, upon further inspection it was clear the code was behaving as intended and indeed it was returning the string this._person.name when it was truthy and '(No name)' when it was not, but how?

Well, my mistake was assuming the || logical operator in JS returned true or false. It doesn’t. Instead it returns the first truthy value.

Logical operators in JavaScript

In JS, both the && and || operators return one of the specified operands. That could be a Boolean or in this case a string object.

My next point of confusion was related to the && operator. Why return this._person.name over this._person if this._person was truthy?

From the MDN Docs on the && Logicial Operator:


If expr1 can be converted to true, returns expr2; else, returns expr1.

And there you have it.

I’ll admit, I’m slightly embarrassed I’ve made it this far into my JavaScript usage without truly understanding how logical || and && worked with non-Boolean objects in JS.

I guess whenever I’ve used an object as an operand for JS logical operators it was always in a case where I was looking for a truthy or falsy value.

Logical operators in Python

The logical or and and behaves the same way in Python too. Consider the following statements:

class Person:
    name = None

person = Person()
person.name = "Michael"
name = person and person.name or "No name"

The value of name is 'Michael'. If I removed the line assigning person.name, the value of name would be 'No name'.

The C# Equivalent

Most of my programming experience is with the static language C#. In C# the logical operators && and || only work with Boolean operands and only return Boolean values. The C# equivalent to using the logical && and || as above would be from using the null coalescing operator with the null conditional operator.

string name = person?.Name ?? "No name";

I think I prefer the use of the ternary operator. I think it more concisely conveys the exact intent of the line of code.

public get name(): string {
    return this._person && this._person.name ? this._person.name : "(No name)";
}

However, leveraging the logical operator is starting to grow on me.

I’ll need to update my 3 JavaScript Gotcha’s for the C# Developer blog post.

CLICK HERE to check out some of our other posts.

A JavaScript Language Update

I felt this blog post should be updated to inform you that a legitimate null coalescing operator is on the horizon for JavaScript! It’s actually called the nullish coalescing operator instead of null. You can view the proposal the nullish coalescing operator for it here. As of writing this update, the proposal is in Stage 4, meaning it is ready for a formal inclusion into the ECMAScript standard.

The nullish coalescing operator checks whether or not an object is null or undefined. In the following expression:

const name = person.name ?? "(No name)";

name will evaluate to “(No name)” if person.name is equal to null or undefined. Otherwise it will be equal to person.name.

FIND US ONLINE!

  • Instagram: @thecodingcouple
  • Twitter: @thecodingcouple
  • GitHub: @thecodingcouple

Related Posts

  • Avoid using mutable values as default parameter values in Python (Today I Learned)Avoid using mutable values as default parameter values in Python (Today I Learned)
  • Creation Crate Month 4: An Arduino powered LED dice gameCreation Crate Month 4: An Arduino powered LED dice game
  • Creation Crate Month 3: An Arduino Powered Distance DetectorCreation Crate Month 3: An Arduino Powered Distance Detector
  • .mjs extension (A JavaScript module file) | Today I Learned.mjs extension (A JavaScript module file) | Today I Learned
  • 3 JavaScript Gotchas for the C# Developer3 JavaScript Gotchas for the C# Developer
  • CSS Magic with a Single DivCSS Magic with a Single Div

Filed Under: JavaScript, Programming Languages, Today I Learned Tagged With: ternary operator

Previous Post: « Tinkering Around with Adafruit’s PyBadge LC
Next Post: Counting Pixels by Color in Python with Pillow (a PIL fork) »

Primary Sidebar

Social Media

  • GitHub
  • Instagram
  • Twitter
  • YouTube

Recent Posts

  • Pokémon Color Picker | A web app built with HTML/CSS + JavaScript
  • Pokéball Single DIV CSS Drawing | Tutorial
  • Error: [🍍]: “getActivePinia()” was called but there was no active Pinia
  • Trijam #261 Game Jam Diary: One Wrong Move
  • Using WSL on Corporate VPN

Recent Comments

  • Lizzy on Creation Crate Month 2: An Arduino Powered Memory Game
  • Ashley Grenon on Creation Crate Month 2: An Arduino Powered Memory Game
  • Lizzy on Creation Crate Month 2: An Arduino Powered Memory Game
  • kelly on Creation Crate Month 2: An Arduino Powered Memory Game
  • Ashley on Creation Crate Month 3: An Arduino Powered Distance Detector

Follow us on Instagram!

This error message is only visible to WordPress admins

Error: No feed found.

Please go to the Instagram Feed settings page to create a feed.

Categories

  • Arduino
  • Conferences
  • Debugging
  • Game Jams
  • HTML and CSS
  • JavaScript
  • Programming Languages
  • Python
  • Raspberry Pi
  • Today I Learned

Archives

  • May 2024
  • April 2024
  • March 2024
  • May 2022
  • December 2021
  • May 2021
  • March 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • June 2019
  • April 2019
  • September 2017
  • April 2017
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • April 2015
  • January 2015

Tags

adafruit arduino brackets c# code smell codestock coding standards conventions creation crate debugging developer devspace electronics es6 es2015 game development game jam gotcha hackathon hoisting html html5 javascript led naming conventions nintendo phaser pluralsight pokemon programmer python raspberry pi retro retropie scope self improvement single div single div drawing subscription box TIL today I learned troubleshooting vue vuejs windbg

Footer

About Us

We are the Coding Couple.  Two people who met in college and decided they wanted to pair program for the rest of their ...

Read More »

Most Recent Posts

Pokémon Color Picker | A web app built with HTML/CSS + JavaScript

Pokéball Single DIV CSS Drawing | Tutorial

Error: [🍍]: “getActivePinia()” was called but there was no active Pinia

Trijam #261 Game Jam Diary: One Wrong Move

Social Media

  • GitHub
  • Instagram
  • Twitter
  • YouTube

Copyright Notice

© The Coding Couple, 2015 – 2023. Excerpts and links may be used, provided that full and clear credit is given to The Coding Couple with appropriate and specific direction to the original content.

Copyright © 2025 · Foodie Pro Theme by Shay Bocks · Built on the Genesis Framework · Powered by WordPress