0

I have the following code in JS:

if (all[j][i][11] != Boolean(1) && $scope.w[j][i] != undefined && $scope.ip[j][i] != undefined && $scope.type[j][i] != undefined)
            var specificArray = []
            specificArray.push($scope.w[j][i]);
            // continue with the code

it gives me the next error:

TypeError: Cannot read property 'push' of undefined

But specificArray is defined just before the push operation.

I also try: var specificArray = new Array()

Or Smith
  • 3,556
  • 13
  • 42
  • 69
  • 1
    See http://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1 – Barmar Dec 31 '14 at 10:37

1 Answers1

4

You should add some curly braces on the condition

if (all[j][i][11] != Boolean(1) && $scope.w[j][i] != undefined && $scope.ip[j][i] != undefined && $scope.type[j][i] != undefined){
    var specificArray = []
    specificArray.push($scope.w[j][i]);
}

Without it the behavior is like this:

if (all[j][i][11] != Boolean(1) && $scope.w[j][i] != undefined && $scope.ip[j][i] != undefined && $scope.type[j][i] != undefined){
    var specificArray = [];
}
specificArray.push($scope.w[j][i]); // Cannot read property 'push' of undefined
Hacketo
  • 4,978
  • 4
  • 19
  • 34