tried destructuring… can anyone please check the code below
Your code so far
function getLength(str) {
"use strict";
// change code below this line
const length = { len : str.length };// change this
const{ len } = length;
// change code above this line
return len; // you must assign length to len in line
}
console.log(getLength('FreeCodeCamp'));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36.
HERE the function is returning the length of the string… " const {length : len} = str "
IS ‘length’ a pre-defined function (str.length) ?
is it a property we have on string ?
Yes that’s right, the functions associated are actually properties of the objects (and arrays and strings) inherited via their prototypes
That’s in a way why we can type str.length or str.split(), split is a property of strings, and is also a function we can call - adding the () actually calls this function
Of course you wouldn’t necessarily want to destructure out the functions in this way, but you can