A barred word in the code itself ?!

Tell us what’s happening:
although the test is running and the code is correct but, I still wonder :
console.log(createPerson(name, 56, "male")) : its result is { name: '', age: 56, gender: 'male' }
(PS: in the code itself the word name IS barred)
console.log(createPerson("John", age, "male")) : its result is age not defined. I expected to have somrthing like this : { name: 'John', age: '', gender: 'male' }
(PS: in the code itself the word age is NOT barred)
console.log(createPerson("John", 56, gender)) : its result is gender not defined . I expected to have { name: 'John', age: 56, gender: '' } just like the first one.
(PS: in the code itself the word gender is NOT barred)
What can these differences possibily mean ?
Your code so far


const createPerson = (name, age, gender) => ({
// Only change code below this line

  name, age, gender

// Only change code above this line
});

console.log(createPerson(name, 56, "male"))
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36 Edg/89.0.774.45.

Challenge: Write Concise Object Literal Declarations Using Object Property Shorthand

Link to the challenge:

what do you mean with this? I don’t understand

anyway, you can’t use the variables name, age, and gender outside the function, as they are not defined in the global scope.
If you want to give an empty string as value you need to do createPerson("", "", ""), this will give {name: "", age: "", gender: ""}

These pictures show how the same mistake (using the variables outside of function) is treated in two different ways ?

It seems that name should not be used as a variable name, because if you delete everything and write console.log(name), it seems to have value of an empty string

it’s in this list of reserved words: JavaScript Reserved Words

1 Like

It just illustrates the problems of global variables:

https://developer.mozilla.org/en-US/docs/Web/API/Window/name

This is the value of the variable, it’s why it’s showing as an empty string rather than erroring. It’s an error in your code. You’ve tried to pass a variable to that constructor function (name) without defining what that variable is, but because window.name is a thing that already exists, it is giving you that confusing result instead of throwing an error.

Following works fine – there’s still a warning in your code, the name is still struck out, but it works as expected:

const name = "abcdef";

const createPerson = (name, age, gender) => ({ name, age, gender });

console.log(createPerson(name, 56, "male"))
// { "name": "abcdef", "age": 56, "gender": "male" }
1 Like