0
var a = null;
function b() {return "B";}
(a || b)();

when i alert((a || b)());.it shows B. why? the return of a || b is true or false. why the above return B.

2:Local Variables

function power(base, exponent) {
var result = 1;
for (var count = 0; count < exponent; count++)
result *= base;
return result;
}
power(2, 10);

a book says

if power were to call itself, that call would cause a new, distinct result variable to be created and used by the inner call and would leave the variable in the outer call untouched.

i can't follow it well, expect someone can explain it to me. many thanks

1 Answer 1

3

1.

The return value of || is not boolean.

It is the first argument if it is truthy or the second argument if it is not.

So a || b is equivalent to a ? a : b and a && b is equivalent to a ? b : a.

2.

When power is called, a new frame is pushed onto the call stack to hold the paramaters and local variables like other languages. But JavaScript is a bit different from many languages in that when a function call results in a new function instance being created, the new function instance holds a reference to the stack frames on the stack when it is created. Since these stack frames hold locals, there is a different place in memory for functions created by different calls to the same function.

For example, in

function makeCounter() {
  var counter = 0;
  return function () { return counter++; };
}

var c1 = makeCounter();
var c2 = makeCounter();
c1(); c1(); c1();
c2(); c2();
alert(c1() + ", " + c2());  // -> 3, 2
alert(c1() + ", " + c2());  // -> 4, 3

makeCounter is first called to initialize c1. This creates a stack frame like { counter: 0 } which the first counter function points to. The second call to makeCounter used to initialize c2 creates a different stack frame.

So the code above is equivalent to

var c1SFrame = { counter: 0 };
var c2SFrame = { counter: 0 };
c1SFrame.counter++; c1SFrame.counter++; c1SFrame.counter++;
c2SFrame.counter++; c2SFrame.counter++; c2SFrame.counter++;
alert(c1SFrame++ + ", " + c2SFrame++);
alert(c1SFrame++ + ", " + c2SFrame++);

which should make it obvious why it alerts what it does.

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

5 Comments

If x=10 and y= 5 then (x=10) || (y=10) return is true .this example is like the above example. why the above return value is not boolean.
@zhuanzhou, no, the result of (x=10) || (y=10) is 10 since the result of both assignments is 10. If you meant to say, (x == 10) || (y == 10) then that would be true because the result of x == 10 is true and true ? true : false is true.
i found the example form this.bton.com/tb16/jsref/operator.html (Comparison and Logical Operators part)so the examplt is wrong? am i right? thank you.
to the question 2, could you make me a simple example? many thanks.
@zhuanzhou, yes. The examples at that website are hopelessly muddled. Adding an example for point 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.