john33
john33

Reputation: 61

Array destructure with an OR operator issue, Uncaught TypeError: foo is not a function

Return true from function foo and structure it with an OR operator.

function foo() {
  return true;
}
var [a, b, c] = foo() || [];

Why it is throwing an error like foo is not a function.

Upvotes: 1

Views: 75

Answers (2)

georg
georg

Reputation: 215009

Looks like a bug in V8 error reporting:

function foo() {
  return true;
} 


var [a] = foo() ; // Uncaught TypeError: foo is not a function or its return value is not iterable
var [a] = foo() || []; // Uncaught TypeError: foo is not a function

Firefox correctly reports "is not iterable" in both cases.

Upvotes: 1

SerShubham
SerShubham

Reputation: 883

The actual error is in fact: TypeError: foo is not a function or its return value is not iterable.

This is because the execution will not reach the OR statement because the result of foo() is not false-y (It is infact true). Hence, javascript tries to destructure true, which gives you a TypeError

Upvotes: 2

Related Questions