1

I have the code:

var here;
function setNews(data2){
    here = data2;
    console.log(here);//1st console log
}
console.log(here);//2nd console log

in the 1st console log the the data inside here are printed but in the 2nd console log it prints undefined how can i access the data inside the setNews function so that I can use it outside setNews.

Thank you.

3
  • Acutally it should work, can you past the entire code? I'm able to reproduce the behaviour. var here; function setNews(data2){ here = data2; console.log(here);//1st console log } setNews("ff"); console.log(here);//2nd console log Commented Apr 28, 2014 at 6:45
  • did you put all this code inside document.ready function? I think your code should work when you put all outside the document.ready. Try that. Commented Apr 28, 2014 at 6:47
  • 1
    Where did you call the function setNews? Commented Apr 28, 2014 at 6:47

6 Answers 6

2

Probably you need to review your architecture.

var here;
function setNews(data2){
    here = data2;
    console.log(here);//1st console log
}
//executed immediatly, `here` is not yet initialized by setNews
console.log(here);//2nd console log

Variable 'here' is being output to the console immedialy when javascript is loaded, but since it's undefined, console shows 'undefined'.

When later you call setNews('sample'), it will set global variable here but there is no point in that, because it was already outputted.

Sign up to request clarification or add additional context in comments.

Comments

1
var here;
function setNews(data2){
    here = data2;
    console.log("inside function " +here);//1st console log
}
setNews("something");
console.log("outside function" +here);//2nd console log

Fiddle: http://jsfiddle.net/bmArj/

Comments

0

// initialize this to desired value.

var here = "your value";

Comments

0

I think...use return...

var here = setNews(2);

function setNews(data2){
    here = data2;
    console.log(here);//1st console log

    return here;
}
console.log(here);//2nd console log

Comments

0

Please read this article on JavaScript Variable and Function Hoisting.

What happened is when you first declare the variable here, it wasn't initialized. When you give here a value inside function setNews(), its value is not available to the outer console.log. So you need to call setNews() first before displaying the content of here in the second call to the console, like so:

var here;
function setNews(data2){
    here = data2;
    console.log(here);//1st console log
}
setNews("some data here");
console.log(here);//2nd console log, it will display "some data here"

Comments

0

If you want to define a variable, (let's call it "here") that is automatically set to the value of some function named "setNews," then this might work better:

var here,
    data2 = "the news!";

// Set value of "here" to processed data2
here = (function (news) {
    // Process news
    news = "This is " + news;
    return news;
})(data2);

console.log(here);
// Prints "This is the news!"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.