Mohamed Alsad
Mohamed Alsad

Reputation: 1

How to print a string that is inside an object in js?

I have an object named promise and it has functions and string inside it. For printing the object i can use

console.log(promise);

but i have a string named "responseText" inside promise. If i try to print it using

console.log(promise.responseText);

,its showing as undefined. I can see the value of responseText by printing the object. But when i print using promise.responseText its showing undefined.

FYI

I am able to print all the functions inside promise, but I am not able to print the string. Please help.

Upvotes: 0

Views: 89

Answers (1)

dev.waqas
dev.waqas

Reputation: 66

Use

var promise = {
  responseText: function(){
    return "responseText"
  }
}

console.log(promise.responseText())

OR

var promise = {
  responseText: function(){
    console.log("responseText")
  }
}

promise.responseText()

Upvotes: 2

Related Questions