Reference Error: job is not defined


#1

https://www.codecademy.com/courses/objects-ii/0/4?curriculum_id=506324b3a7dffd00020bf661#

Hi, I keep getting a reference error that reads : job is not defined.

Also, please be elaborate about the error. I am a beginner, hence understanding why errors occur will help me to avoid them in future.


var james = {
    job: "programmer",
    married: false,
    sayJob: function() {
        // complete this method
        this.job = job;
        console.log("Hi, I work as a " + job);
    }
};

// james' first job
james.sayJob();

// change james' job to "super programmer" here
james.job = "super programmer";
// james' second job
james.sayJob();
.


#2

Hi, I configured the code to this

var james = {
    job: "programmer",
    married: false,
    sayJob: function() {
        // complete this method
        this.job = james.job
        console.log("Hi, I work as a " + james.job);
    }
};

// james' first job
james.sayJob();

// change james' job to "super programmer" here
james.job = "super programmer";
// james' second job
james.sayJob();

The console on the codecademy is not displaying the result, but I typed this in my browser's console and got the two lines printed, but I also got an undefined error.

Can someone explain this undefined error?


#3

undefined in your code is not an error it only means that your function is not returning anything.
for instance
function action(){
console.log("my action figure");
}
action();
action above logs the string "my action figure" and > undefined.
this is becos i'm not returning any value;
if i ran
function action(){
return "my action figure";
}
action() <--- will return the string "my action figure";

Computers parse code line by line so after it gets to your console.log();
it parses it, logs the value and moves to the next line. On the next line,
it discovers that no value exists so it returns > undefined

now, the second code does not return undefined becos the function becomes a store house
for your code. Here your function is likened to a variable which stores a value it whether string or number
just like var x = "game"; noting follows after that, its just like a store house where you say keep this value for me
i'll use it later. Now the computer knows not to parse any thing after that line of code. It just stores it. That is why nothing
ever runs after you use the return syntax in your function.

Hope this was helpful.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.