1

The result of the code below is Cat and undefined. *If the key does not exist, the result will be "undefined".

   var animals = {"mammals":["Cat","Dog","Cow"]};
   var groupA  = animals.mammals[0];
   var groupB  = animals.birds;
   console.log(groupA );
   console.log(groupB);

However, The result of the below code is "error" instead of "undefined".

*Uncaught TypeError: Cannot read properties of undefined (reading '0')

var animals = {
  "mammals": ["Cat", "Dog", "Cow"]
};
var groupA = animals.mammals[0];
var groupB = animals.birds[0];

console.log(groupA);
console.log(groupB);

If the key have an ordinal number which doesn't exist, How to get an "undefined" ?

isherwood
  • 58,414
  • 16
  • 114
  • 157
saya-ma
  • 33
  • 2
  • 1
    `.birds;` is undefined since well, it is not defined anywhere and thus an element 0 of undefined `.birds` does not exist so referencing it is an error – Mark Schultheiss Oct 01 '21 at 17:13
  • By "escape" do you mean avoid, handle, prevent or really actually "escape" that as in "The `escape()` function computes a new string in which certain characters have been replaced by a hexadecimal escape sequence." – Mark Schultheiss Oct 01 '21 at 17:20

5 Answers5

3

You can use optional chaining

The optional chaining operator (?.) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.

var animals = {
  "mammals": ["Cat", "Dog", "Cow"]
};
var groupA = animals.mammals?.[0];
var groupB = animals.birds?.[0];

console.log(groupA);
console.log(groupB);
Junius L
  • 15,881
  • 6
  • 52
  • 96
3

You need to evaluate the attribute first, see below:

var animals = {"mammals":["Cat","Dog","Cow"]};
var groupA  = animals.mammals[0];
var groupB  = (animals.birds || [])[0];
Giusseppe
  • 147
  • 7
2

You could check for the property and explicitly set that value with a ternary:

var animals = {
  "mammals": ["Cat", "Dog", "Cow"]
};
var groupA = animals.mammals ? animals.mammals[0] : undefined;
var groupB = animals.birds ? animals.birds[0] : undefined;

console.log(groupA);
console.log(groupB);
isherwood
  • 58,414
  • 16
  • 114
  • 157
2

In second case: birds itself is undefined on the object animals. Accesing property of undefined is throwing the error.

It is like doing undefined[0]. Use optional chaining

var animals = {
  "mammals": ["Cat", "Dog", "Cow"]
};
var groupA = animals.mammals?.[0];
var groupB = animals.birds?.[0];

console.log(groupA);
console.log(groupB);
Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
1

Use try...catch blocks to catch exceptions and handle them as needed (surround anything you think might throw errors):

const animals = {
  "mammals": ["Cat", "Dog", "Cow"]
};
let groupA, groupB, groupC;
groupA = animals.mammals[0];
groupB = animals.birds;
try {
  groupC = animals.birds[0];
} catch (error) {
  console.error(
    "'animals.birds' is undefined, so we cannot access property '0'."
  );
}
console.log(groupA);
console.log(groupB);
console.log(groupC);
phentnil
  • 2,195
  • 2
  • 14
  • 22