JavaScript: Pass value of x into function

How can I pass on the value of x into this function?

var x = 'Hello'; = function() {alert(x) }

Open in new window

LVL 16
Who is Participating?
Meir RivkinConnect With a Mentor Full stack Software EngineerCommented:
Sorry my mistake, u need to assign x to 'this' variable:
var me = this;
var x = 'Hello';
me.x=x; = function() {alert(me.x); }
Meir RivkinFull stack Software EngineerCommented:
var me = this;
var x = 'Hello'; = function() {alert(me.x); }
hankknightAuthor Commented:
That does NOT work.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.