Basic JavaScript - Testing Objects for Properties - Bracket notation (property name wih a space, value as string)

Tell us what’s happening:

Describe your issue in detail here.
Hola, given return obj[checkProp]; we state that ‘checkProp’ is a string (not a number?) so my question is, are we only considering strings as property values?

Your code so far

function checkObj(obj, checkProp) {
  // Only change code below this line
  if(obj.hasOwnProperty(checkProp)) {
    return obj[checkProp];
    } else {
        return 'Not Found'
  // Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:120.0) Gecko/20100101 Firefox/120.0

Challenge Information:

Basic JavaScript - Testing Objects for Properties

Hi @PetruX, welcome back to the forums!

In this instance, when dealing with property values, the string checkProp is standing in for the name of a property on an object. That type of dynamicism allows for checking/accessing the property of an object without manually typing in that property name. It’s useful for checking if properties exist in one object that don’t exist in another or do exist in another. There are many use cases for this type of dynamic property access!


And I should actually answer your question and say that, no, strings are not the only valid way to check for a property.

In fact, since arrays are technically objects in JavaScript, you can use a number variable to try to check to see if an index exists in that array and then do something with that information. You can use the same hasOwnProperty that you use on a regular object with an array since it has access to all of the same object methods.

For example,

const myArr = [7,8,9];
const checkIndex = 2;

if (myArr.hasOwnProperty(checkIndex)) {
  console.log(`the value at myArr at index ${checkIndex} is ${myArr[checkIndex]}`);
else {
  console.log(`I couldn't find anything at index ${checkIndex}`);

In this instance, it’ll output that 9 was found at index 2. But if you update the checkIndex variable here to be anything beyond 2, it will output the else portion and say that it couldn’t find anything at index checkIndex.

You may see this syntax for arrays:

//check to see if the checkIndex index comes back as a truthy value
if (myArr[checkIndex]) {
  console.log(`the value at myArr at index ${checkIndex} is ${myArr[checkIndex]}`);
else {
  console.log(`I couldn't find anything at index ${checkIndex}`);

There is a major caveat with this in that values like 0 will be treated as though they don’t exist, which is why hasOwnProperty is the better method to use.


Hey, thank you for such an answer this fast.
So, ‘checkIndex’ as a string, not as a number?

Like I said, in that particular example I created, checkIndex is a number since it is dealing with arrays, and arrays have ordinally numbered indices starting at 0 and going up for every item in the array.

Think of it this way, an array really looks like this behind the scenes:

myArr = {
  0: 7,
  1: 8,
  2: 9

Notice that the property names are numbers here. At its core, it’s really just an object with numbers as the property names instead of strings.

However, we don’t initialize arrays like that because arrays specifically have special functionality since they deal with ordinal pairs of numbers (numbers that go in sequence) that make them more efficient to deal with when dealing with lists of things.


I am reviewing the use of ’ [ ] { } ( )’ right now.
Thank you for the lessons


any source as a suggestion?
My issue is to understand the right use of symbols stated above. Perhaps you know a useful (enough) source?

Go through the lessons here multiple times, and just write code because those will eventually sink in with lots of practice. Those symbols change with context because it depends on what you’re trying to do as to what symbols you should use. And work on the projects because that will be something you are working on by yourself (and help from the community as you need it).


Sorry I am not used to a learning coding forum working this well.
Muchas gracias Marcus!


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.