Destructuring objects - how do this code calculate the str length we are not using str.length?

function getLength(str) {
“use strict”;

// change code below this line
const {length : len} = str; // change this
// change code above this line

return len ; // you must assign length to len in line



how do this code calculate the str length we are not using str.length

This is how i explained to another camper on another post:

so length here is keyword not constant or variable. why dont we use str.length or ojha.length it is much easier and less confusing

It is just trying to teach you how you could use destructuring.

Also, in the future, please use a short description in your topic’s title of what help you need instead of pasting a url.

If you had the following object:

const myObj = {
  name: 'John',
  age: 40,
  city: 'San Jose',
  state: 'California',
  salary: 100000

and wanted to assign each property’s value to a variable of the same name as the property, you could write:

const name =;
const age = myObj.age;
const city =;
const state = myObj.state;
const salary = myObj.salary;

OR, use destructuring like:

const {name, age, city, state, salary} = myObj;

Which is simpler to you?

As @RandellDawson
explained, you’ll see benefits on larger objects. Purpose of this exercise is to show you on simplest example as possible.

I changed your topic title to reflect what your topic was really about instead of having a url.