Evaluating code in the console

I am using the console to evaluate some code and I really cannot understand something.The code you are about to see has to do with empty input elements in the DOM,actually some filled and some empty…the last of them is empty so the code applies to it.Look at the fiddle here to understand what I am talking about.Click edit and click the plus icon to add an empty input field…
Take a look at this code:

var inputHasValue = function(index) {
		return $('.services:last').val() === '';
	};
//it returns undefined

and now take a look at this:

$('.services:last').val()==='';
//it returns TRUE...which is the correct

Why the first code-which is a function- returns undefined?

When I console.log inputHasValue, it’s not undefined, it’s a function that hasn’t yet been executed.

I do not quite understand how your answer solves my problem…I assume that when I hit enter in the console the function is executed.Am I right?

No, because jsfiddle is what you get from the browser console.
The results section where you see your rendered page is inside of an iframe, and I don’t think that you can use console to easily access that. As such, in that environment other techniques must be used instead.

Well…I must emphasize(and sorry for not doing it earlier) that the evaluation/testing of the code you see above was made in a local machine…so I am confident in what I say.

How did you determine that the function returns undefined?

When I modify the function to use a local variable, the console shows that it’s always returning true or false.

  inputHasValue = function(index) {
    var hasValue = $(this).val() === '';
    console.log(hasValue);
    return hasValue;
  };

Ι hit enter in the console.

Yes…but you have to modify the function to get the value.
Can I get the value with the way the function it is originally?I want to avoid complicating the function just for the sake of getting the value

That’s right, and it’s a technique that is guaranteed to work.

This is starting to feel like a flat-earth vs globe discussion. Before I waste my time further, please tell me which other working techniques are not allowed to be used either.

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