Sling Academy
Home/JavaScript/JavaScript: Checking if a Key/Value Exists in an Object

JavaScript: Checking if a Key/Value Exists in an Object

Last updated: March 20, 2023

This article shows you how to check whether a key or a value exists in a given object in JavaScript.

Checking If a Key Exists

The in operator can be used to determine if a key exists in an object.

Example:

const obj = {
    key1: 'value1',
    key2: 'value2',
    key3: 'value3'
}

if("key1" in obj) {
    console.log("key1 is a key of obj")
} else {
    console.log("key1 is NOT a key of obj")
}

if("key4" in obj){
    console.log("key4 is a key of obj")
} else {
    console.log("key4 is NOT a key of obj")
}

Output:

key1 is a key of obj
key4 is NOT a key of obj

You can also use the hasOwnProperty() method to check if a key belongs to the object itself and is not inherited from another object:

if (obj.hasOwnProperty("someKey")) { 
  // do something
}

Checking If a Value Exists

We can use the Object.values() method to get an array of the object’s values and then use the indexOf() or includes() method to check if a value exists in that array.

Example:

const obj = {
    key1: 'value1',
    key2: 'value2',
    key3: 'value3'
}

// check if "value3" is in the object
if (Object.values(obj).includes('value3')) {
    console.log('value3 is in the object')
} else {
    console.log('value3 is not in the object')
}

// check if "value10" is in the object
if (Object.values(obj).includes('value10')) {
    console.log('value10 is in the object')
} else {
    console.log('value10 is not in the object')
}

Output:

value3 is in the object
value10 is not in the object

That’s it. Happy coding!

Next Article: JavaScript: Create an Object from Arrays of Keys and Values

Previous Article: JavaScript: Create/Access Object Properties with Variable Keys

Series: Working with Objects in JavaScript

JavaScript

You May Also Like

  • Can JavaScript programmatically bookmark a page? (If not, what are alternatives)
  • Dynamic Import in JavaScript: Tutorial & Examples (ES2020+)
  • JavaScript: How to implement auto-save form
  • JavaScript: Disable a Button After a Click for N Seconds
  • JavaScript: Detect when a Div goes into viewport
  • JavaScript Promise.any() method (basic and advanced examples)
  • Using logical nullish assignment in JavaScript (with examples)
  • Understanding WeakRef in JavaScript (with examples)
  • JavaScript Numeric Separators: Basic and Advanced Examples
  • JavaScript: How to Identify Mode(s) of an Array (3 Approaches)
  • JavaScript: Using AggregateError to Handle Exceptions
  • JavaScript FinalizationRegistry: Explained with Examples
  • JavaScript String replaceAll() method (with examples)
  • Nullish Coalescing in JavaScript: A Developer’s Guide
  • JavaScript Promise.allSettled() method (with examples)
  • JavaScript: Checking if the current window is a popup
  • JavaScript: Checking if the current window is fullscreen
  • JavaScript: Checking if a Div element is visible
  • JavaScript: How to programmatically reload/close the current page