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

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

#1

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects

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

}

console.log(getLength(‘FreeCodeCamp’));

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


#2

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


#3

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


#4

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.


#5

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 = myObj.name;
const age = myObj.age;
const city = myObj.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?


#6

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


#7

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