How to handle an undefined key in JavaScript ?
Last Updated :
31 May, 2022
In this article, we will try to analyze how we may handle an undefined key (or a property of an object) in JavaScript using certain techniques or approaches (via some coding examples).
Firstly let us quickly analyze how we may create an object with certain keys along with their values using the following syntax:
Syntax:
let object_name = {
property_name : value,
...
}
Let us now see the following illustrated quick example which will help us to understand the object creation syntax:
Example: In this example, we will simply create an object with certain properties having certain values in them.
JavaScript
<script>
let employee_details = {
firstName: "ABC",
lastName: "DEF",
age: 22,
designation: "Technical Content Writer",
organizationName: "GeeksforGeeks",
};
console.log(employee_details);
console.log(
`Complete name of an employee is
${employee_details.firstName}
${employee_details.lastName}`
);
</script>
Output:
{
firstName: 'ABC',
lastName: 'DEF',
age: 22,
designation: 'Technical Content Writer',
organizationName: 'GeeksforGeeks'
}
Complete name of an employee is ABC DEF
Now as we have understood the object creation process, let us see some of the following illustrated approaches/ conditions/ cases through which we would easily understand the problem statement (which is handling of an undefined key):
Approach 1: In this approach, we will use the same object that we have created in the previous example and will further try to access a key that is not defined in that object and also will handle the error.
Example 1:
JavaScript
<script>
let employee_details = {
firstName: "ABC",
lastName: "DEF",
age: 22,
designation: "Technical Content Writer",
organizationName: "GeeksforGeeks",
};
console.log(
`Address of an employee is :
${employee_details?.address ?? "not found"}`
);
</script>
Output:
Address of an employee is : not found
Example 2:
JavaScript
<script>
let employee_details = {
firstName: "ABC",
lastName: "DEF",
age: 22,
designation: "Technical Content Writer",
organizationName: "GeeksforGeeks",
};
console.log(
`Address of an employee is :
${"address" in employee_details ?
employee_details.address :
"not found"
}`
);
</script>
Output:
Address of an employee is : not found
Approach 2: In this approach, we will use the same object but here we will add one more key (or property) explicitly with the value as "undefined" and then will see the output while accessing it.
Example:
JavaScript
<script>
let employee_details = {
firstName: "ABC",
lastName: "DEF",
age: 22,
designation: "Technical Content Writer",
organizationName: "GeeksforGeeks",
address: undefined,
};
console.log(
`Address of an employee is : ${
employee_details?.address === undefined
? "not defined"
: employee_details.address
}`
);
</script>
Output:
Address of an employee is : not defined
Similar Reads
How to Detect an Undefined Object Property in JavaScript ? Detecting an undefined object property is the process of determining whether an object contains a certain property, and if it does, whether the value of that property is undefined. This is an important concept in JavaScript programming, as it helps to prevent errors that can occur when attempting to
3 min read
How to check for "undefined" value in JavaScript ? In JavaScript, undefined is a primitive value that represents the absence of a value or the uninitialized state of a variable. It's typically used to denote the absence of a meaningful value, such as when a variable has been declared but not assigned a value. It can also indicate the absence of a re
2 min read
How to get the first non-null/undefined argument in JavaScript ? There are many occasions when we get to find out the first non-null or non-undefined argument passed to a function. This is known as coalescing. Approach 1: We can implement coalescing in pre-ES6 JavaScript by looping through the arguments and checking which of the arguments is equal to NULL. We the
5 min read
Undefined in JavaScript Undefined is a type of Data type in JavaScript. It is a primitive value undefined, when a variable is declared and not initialized or not assigned with any value. By default, the variable was stored with an Undefined value. Undefined is a global read-only variable that represents the value Undefined
2 min read
What does !== undefined mean in JavaScript ? In JavaScript, !== is a strict inequality operator, and undefined is a special value representing the absence of a value or the lack of an assigned value to a variable. The !== operator checks for both value and type equality, and !== undefined is used to verify if a variable is not equal to the und
1 min read
How to store a key=> value array in JavaScript ? In JavaScript, storing a key-value array means creating an object where each key is associated with a specific value. This allows you to store and retrieve data using named keys instead of numeric indices, enabling more intuitive access to the stored information.Here are some common approaches:Table
4 min read