-1

This gives correct answer:

<script type="text/javascript">
var numbers=[67,56,45,34,78,54,67,90,43,56,78,90,23,45,67,89,54,1];
var sita=0;
for(i=0;i<numbers.length;i++){
    if(numbers[i]>sita){
        var sita=numbers[i];
        document.write(sita+" ");
        }
    }
</script>

This is not working:

<script type="text/javascript">
var numbers=[67,56,45,34,78,54,67,90,43,56,78,90,23,45,67,89,54,1];
for(i=0;i<numbers.length;i++){
    if(numbers[i]>sita){
        var sita=numbers[i];
        document.write(sita+" ");
        }
    }
</script>

why?

Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
LIGHT
  • 5,604
  • 10
  • 35
  • 78

3 Answers3

2

as others have mentioned sita is undefined in the second example.

a larger or smaller comparison against undefined - in that specific situation of yours - always yields false, no matter against what you compare.

so, your expression translates to

if (false)

EDIT: I completely missed line 5 of the second example because so many people wrote that the variable sita was undefined when in fact just it's value is undefined. So enabling strict mode won't do much good here. Anyways, just for reference, my original post:

To avoid mistakes like that you should always (or if not always then at least while debugging) use the strict mode (available since ECMAScript 5).

"use strict";

link: What does "use strict" do in JavaScript, and what is the reasoning behind it?

Community
  • 1
  • 1
lightxx
  • 1,037
  • 2
  • 11
  • 29
  • This is not proper answer, because in this case sita itself is undefined, not value of sita, so it is not equivalent of `if(false)`, instead it will throw exception – karaxuna Jul 26 '13 at 08:22
  • write this in console: `undeclaredvariable > 5`. it will throw exception. then try this: `var second; second > 5;` it will log false. then write `typeof undeclaredvariable` and `typeof second` both will be "undefined". see difference? :) – karaxuna Jul 26 '13 at 08:35
  • I am talking about the specific example of the OP. try it. copy it to an html page. run it. tell me WHERE EXACTLY an error is thrown and reevaluate if i deserved that downvote. http://jsbin.com/uhawub/1/ – lightxx Jul 26 '13 at 08:44
  • @MichaelGeary `if(numbers[i]>sita){ var sita=numbers[i]; ...` sita is declared after it is used in if statement, so I thought sequence matters, but appears I was wrong. My apologies :) – karaxuna Jul 26 '13 at 08:50
  • 2
    We are all learning here something new every day, this was important fact for me, thanks – karaxuna Jul 26 '13 at 08:51
  • 2
    @karaxuna - I deleted my previous comment by mistake, sorry! Here's a repeat so no one gets confused reading the thread. :-) In the second script in the question, there is a `var sita` in the middle, and that means that at the beginning of the script the sita variable does exist, has the `undefined` value, and will not give a reference error. When a function or global script is executed, all variables come into existence before any of the code is executed (commonly called "hoisting"). The effect is as if the `var sita` (but not the assignment) is moved to the top of the script. – Michael Geary Jul 26 '13 at 08:57
0

In your first code example sita is defined just before the for and within your if statement. I would assume that your second definition var sita = numbers[i] should look like sita = numbers[i].

In your second code example sita is not defined before use - only within your if. So sita is undefined and your if condition won't check out and nothing will be printed.

Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
0

Say you're the interpreter. And you've reached the line if(numbers[i]>sita){ what would you think, what value has the sita variable?

You have to define variabls before using them (like in the first example). Otherwise interpreter won't know what means that word.

shift66
  • 11,760
  • 13
  • 50
  • 83
  • That's not quite correct. In the second example, the `sita` variable *does* have a value, `undefined`, at the very beginning of the script - even before the `var sita = 0;` statement. Remember that all variables in a function or script come into existence before any of the code is actually executed. (Commonly called "hoisting".) – Michael Geary Jul 26 '13 at 08:47