You can turn the values of an Object into an array and test that a string is present. It assumes that the Object is not nested and the string is an exact match:

var obj = { a: 'test1', b: 'test2' };
if (Object.values(obj).indexOf('test1') > -1) {
   console.log('has test1');
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values

Answer from Matt Pileggi on Stack Overflow
Top answer
1 of 16
704

You can safely use the typeof operator on undefined variables.

If it has been assigned any value, including null, typeof will return something other than undefined. typeof always returns a string.

Therefore

if (typeof maybeObject != "undefined") {
   alert("GOT THERE");
}
2 of 16
50

There are a lot of half-truths here, so I thought I make some things clearer.

Actually you can't accurately tell if a variable exists (unless you want to wrap every second line into a try-catch block).

The reason is Javascript has this notorious value of undefined which strikingly doesn't mean that the variable is not defined, or that it doesn't exist undefined !== not defined

var a;
alert(typeof a); // undefined (declared without a value)
alert(typeof b); // undefined (not declared)

So both a variable that exists and another one that doesn't can report you the undefined type.

As for @Kevin's misconception, null == undefined. It is due to type coercion, and it's the main reason why Crockford keeps telling everyone who is unsure of this kind of thing to always use strict equality operator === to test for possibly falsy values. null !== undefined gives you what you might expect. Please also note, that foo != null can be an effective way to check if a variable is neither undefined nor null. Of course you can be explicit, because it may help readability.

If you restrict the question to check if an object exists, typeof o == "object" may be a good idea, except if you don't consider arrays objects, as this will also reported to be the type of object which may leave you a bit confused. Not to mention that typeof null will also give you object which is simply wrong.

The primal area where you really should be careful about typeof, undefined, null, unknown and other misteries are host objects. They can't be trusted. They are free to do almost any dirty thing they want. So be careful with them, check for functionality if you can, because it's the only secure way to use a feature that may not even exist.

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-check-if-a-property-exists-in-a-javascript-object
How to Check if a Property Exists in a JavaScript Object
April 25, 2022 - We can check if a property exists in the object by checking if property !== undefined. In this example, it would return true because the name property does exist in the developer object.
๐ŸŒ
Sabe
sabe.io โ€บ blog โ€บ javascript-check-if-value-exists-in-object
How to Check if a Value Exists in an Object in JavaScript
September 10, 2022 - We can check if a value exists in an object by first getting all of the values with Object.values() and then using the includes() method. JAVASCRIPTconst object = { name: "John", age: 30 }; if (Object.values(object).includes("John")) { ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Object โ€บ hasOwn
Object.hasOwn() - JavaScript | MDN
const object = { prop: "exists", }; console.log(Object.hasOwn(object, "prop")); // Expected output: true console.log(Object.hasOwn(object, "toString")); // Expected output: false console.log(Object.hasOwn(object, "undeclaredPropertyValue")); // Expected output: false ... The JavaScript object instance to test. ... The String name or Symbol of the property to test. true if the specified object has directly defined the specified property.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-check-whether-an-object-exists-in-javascript
How to check whether an object exists in JavaScript?
In the example below, we have created the phone object, which contains some properties and values as a key-value pair. After that, we used the if-else statement to check if the phone object exists in the code. Users can observe the output that control goes to the if block as a phone object exists.
Top answer
1 of 5
15

Let's say we start with

const obj = {foo : "bar"};

Check for a value:

const hasValue = Object.values(obj).includes("bar");

Check for a property:

// NOTE: Requires obj.toString() if key is a number
const hasProperty = Object.keys(obj).includes("foo");

Multi-level value:

function hasValueDeep(json, findValue) {
    const values = Object.values(json);
    let hasValue = values.includes(findValue);
    values.forEach(function(value) {
        if (typeof value === "object") {
            hasValue = hasValue || hasValueDeep(value, findValue);
        }
    })
    return hasValue;
}

Multi-level property:

function hasPropertyDeep(json, findProperty) {
    const keys = Object.keys(json);
    let hasProperty = keys.includes((findProperty).toString());
    keys.forEach(function(key) {
        const value = json[key];
        if (typeof value === "object") {
            hasProperty = hasProperty || hasPropertyDeep(value, findProperty);
        }
    })
    return hasProperty;
}
2 of 5
4

No, there is no built in method to search for a value on an object.

The only way to do so is to iterate over all the keys of the object and check each value. Using techniques that would work even in old browsers, you can do this:

function findValue(o, value) {
    for (var prop in o) {
        if (o.hasOwnProperty(prop) && o[prop] === value) {
            return prop;
        }
    }
    return null;
}

findValue(car, "2011");    // will return "year"
findValue(car, "2012");    // will return null

Note: This will return the first property that contains the search value even though there could be more than one property that matched. At the cost of efficiency, you could return an array of all properties that contain the desired value.

Note: This uses the extra .hasOwnProperty() check as a safeguard against any code that adds enumerable properties to Object.prototype. If there is no such code and you're sure there never will be, then the .hasOwnProperty() check can be eliminated.

Find elsewhere
๐ŸŒ
30 Seconds of Code
30secondsofcode.org โ€บ home โ€บ javascript โ€บ object โ€บ object has value or key
Check if a JavaScript object has a given value or key - 30 seconds of code
February 10, 2024 - While the JavaScript API for objects ... task. In order to check if an object has a given value, you can use Object.values() to get all the values of the object....
๐ŸŒ
MyTutor
mytutor.co.uk โ€บ answers โ€บ 10947 โ€บ Mentoring โ€บ Javascript โ€บ How-to-check-if-value-exists-in-an-object-in-Java-Script
How to check if value exists in an object in JavaScript?
Let's consider an example which would answer this question var priceOfFood = { pizza: 14, burger 10 } priceOfFood.hasOwnProperty('pizza') // true priceOfFood['pizza'] // 14 priceOfFood.hasOwnProperty('chips') // false priceOfFood['chips'] // undefined in comments we have the returned value ...
๐ŸŒ
Code Boxx
code-boxx.com โ€บ home โ€บ 2 ways to check if value exists in javascript object
2 Ways To Check If Value Exists In Javascript Object
June 15, 2023 - Welcome to a quick tutorial on how to check if a value exists in an object in Javascript. So you have probably tried to do a includes("VALUE") to check if an object contains a certain value.
๐ŸŒ
CodeParrot
codeparrot.ai โ€บ blogs โ€บ check-if-a-key-exists-in-a-javascript-object
Ways to Check If a Key Exists in a JavaScript Object
One of the simplest ways to check if a key exists in a JavaScript object is by using the in operator.
๐ŸŒ
JavaScript Tutorial
javascripttutorial.net โ€บ home โ€บ 3 ways to check if a property exists in an object
3 Ways to Check If a Property Exists in an Object in JavaScript
June 21, 2020 - How to check if a property exists in an object in JavaScript by using the hasOwnProperty() method, the in operator, and comparing with undefined.
๐ŸŒ
Suraj Sharma
surajsharma.net โ€บ blog โ€บ check-object-in-javascript-array
How to check if a value exists in an Array of Objects in JavaScript | Suraj Sharma
You can use the some() method to check if an object is in the array. users.some(function (u) { if (u.username === user.username) return true; return false; }) // false // using an arrow function users.some(u=>u.username === user.username) // ...
๐ŸŒ
Logfetch
logfetch.com โ€บ js-check-value-exists-in-object
How to Check if Value Exists in an Object in JavaScript - LogFetch
const doesExist = (obj, value) => { for (let key in obj) { if (obj[key] === value) { return true; } } return false } let exists = doesExist(obj, "Bob"); We can also use Object.keys() and some(). let exists = Object.keys(obj).some(key => obj[key] ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-check-whether-an-object-exists-in-javascript
How to check whether an object exists in javascript ? - GeeksforGeeks
July 12, 2025 - Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-check-if-object-value-exists-not-add-a-new-object-to-array-using-javascript
How Check if object value exists not add a new object to array using JavaScript ?
") console.log(checkName('JavaScript')); console.log(" Does the object HTML exist in the array? ") console.log(checkName('HTML')); Step 1 ?Define an array ?inputArray' and add key value pair values to it. Step 2 ?Define a function ?checkName' that takes a string as a parameter. Step 3 ?In the function, use the function some() to check if the given value exists in the array.
๐ŸŒ
EnableGeek
enablegeek.com โ€บ home โ€บ javascript check if value exists in array of objects
Javascript Check if Value Exists in Array of Objects - EnableGeek
August 7, 2023 - The some() method returns a Boolean value indicating whether at least one element in the array satisfies the provided testing function. You can use it to check if a value exists in an array of objects by defining a testing function that checks ...