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
How to get the Value by a Key in JavaScript Map? JavaScript Map is a powerful data structure that provides a convenient way to store key-value pairs and retrieve values based on keys. This can be especially useful when we need to associate specific data with unique identifiers or keys.Different Approaches to Get the Value by a Key in JavaScript Ma
3 min read
How to Check an Object is Empty using JavaScript? These are the following ways that can be used to Check an Object is Empty using JavaScript: 1. Using Object.keys() Method - Mostly usedThe Object.keys() method returns an array that contains the property names of an object. If the length of array is 0, then object is empty.JavaScriptlet obj = {}; if
2 min read
How to get a key in a JavaScript object by its value ? To get a key in a JavaScript object by its value means finding the key associated with a specific value in an object. Given an object with key-value pairs, you want to identify which key corresponds to a particular value, often for searching or data retrieval.How to get a key in a JavaScript object
4 min read
How to Replace a value if null or undefined in JavaScript? In JavaScript, replacing a value if it is null or undefined involves providing a default value to use when the original value is either null or undefined. This ensures the application has valid data, preventing potential errors or unexpected behavior. Here we have some common approaches: Table of Co
2 min read