Aliases with JavaScript Destructuring

By  on  

Destructuring in JavaScript has totally changed the way JavaScript is written these days;  code is more concise to write but but, from a visual standpoint, the syntax of the language has changed so much.  Any good developer knows, however, that change is the constant we live in.

The basic idea behind destructuring in object literals is as follows:

const obj = { x: 1 };

// Grabs obj.x as { x }
const { x } = obj;

There are cases where you want the destructured variable to have a different name than the property name; in that case, you'll use a : newName to specify a name for the variable:

// Grabs obj.x as as { otherName }
const { x: otherName } = obj;

The syntax for specifying an alternate destructured name for an object property is simple and needed.  Destructuring had the capability to confuse developers, especially array destructuring and function argument destructuring, but this alias syntax is a simple trick to keep in your locker!

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

Incredible Demos

  • By
    HTML5 Datalist

    One of the most used JavaScript widgets over the past decade has been the text box autocomplete widget.  Every JavaScript framework has their own autocomplete widget and many of them have become quite advanced.  Much like the placeholder attribute's introduction to markup, a frequently used...

  • By
    Scrolling “Go To Top” Link Using Dojo

    One of the most popular code snippets of posted on my blog has been the scrolling "Go To Top" link snippet. The premise of the snippet is simple: once the user scrolls an element (usually the BODY element) past a given threshold, a "Go...

Discussion

  1. Always have to check your site first to see any updates. Love it david. That’s where good developers stand out. Always adapting to new changes. Just trying to get hang of destructing as i’m not the smartest one in the room.

  2. undefined alias

    Hello. I’m using an alias for my destructed object property. How can I handle the property when it’s undefined?
    Thanks.

  3. Jan

    I was looking for something like this. I wonder why the proposal for this feature didn’t follow the syntax of import statements.

    Example:

    const { x as otherName } = obj;
    
    • Memo

      this is the typescript syntax

  4. mark

    I don’t know why people call it an “alias”. If it were an alias, changing the variable would change the object property. E.g.

    let obj = { x: 1 }        
    let { x: x_notalias } = obj 
    x_notalias = 2                  // 2
    obj                             // { x: 1 }
    

    I think the only time you can have an alias in JavaScript is with reference types.

    let obj = { x: 1 }
    let obj_alias = obj
    obj_alias.x = 2
    obj                        // { x: 2 }
    
  5. Maksym Kulikovskiy

    When destructuring we use the word “alias” to refer to a differently named variable with the same value, rather than a different name that should reference the same variable.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!