JsCoder
JsCoder

Reputation: 1733

Javascript variable evaluation

(function(){

   var commentList = $("#commentList");

});

Given the context above will commentList get evaluated every time use the variable?

Upvotes: 0

Views: 1944

Answers (4)

Flash
Flash

Reputation: 16713

It is evaluated once, when the assignment takes place.

Demo: http://jsfiddle.net/PxRXF/

Upvotes: 2

Alexander Larikov
Alexander Larikov

Reputation: 2339

No. You can easily check it

<script>
$(function(){

   var commentList = $("#commentList");
   console.log(commentList);
   $('#commentList').html('');
   console.log(commentList);

});
</script>

<div id="commentList">Test</div>

Upvotes: 2

Mitya
Mitya

Reputation: 34556

It will be evaluated afresh each time the function is called.

Once inside the function, it will be evaluated once, not each time the var is called.

Upvotes: 2

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123397

no, that variable will store a reference to it, so every time you use commentList you won't also re-evaluate $("#commentList") (except for the first assignment, of course)

Upvotes: 2

Related Questions