"use strict";
const data = {
firstName: "myName",
lastName: this.firstName,
};
console.log(data.lastName);
Output is undefined. Can anyone explain why it's happening?
Because this does not work like that. When you are defining your object literal (data), this does not point to that literal - to see what this actually is, do a console.log(this).
You can solve your problem by modifying your code like this:
"use strict";
const data = {
firstName: "myName"
}
data.lastname = data.firstname;
Also, I recommend reading up on how this works to avoid confusion in the future - this is a bit weird in Javascript.