Warning: session_start(): open(/tmp/sess_1dcae12200e1e23923914265c90d678a, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Check If A Key Exists In A JavaScript Object - LearnShareIT

How To Check If A Key Exists In A JavaScript Object

Check if a key exists in a javaScript object

Some methods can help you check if a key exists in a JavaScript object. This tutorial will show you how to handle it through the explanation and examples below.

Check If A Key Exists In A JavaScript Object.

What is an object in JavaScript?

For example, when you see a dog. A dog is an object with properties. A dog has a weight, species, color, etc.

JavaScript objects have properties that define their characteristics. You can specify a property by assigning it a value. 

Look at the example below.

let dog = {
   weight: 30,
   species: "Pitbull",
   color: "Yellow"
};

Using in operator

Syntax

prop in objectName

Parameters

  • prop: the name of the property you need to check.
  • objectName: the name of the object in which you need to check if a key exists.

Return value

  • True if the prop exists in objectName.
  • False if the prop doesn’t exist in objectName.

Look at the example below.

const dog = {
   weight: 30,
   species: "Pitbull",
   color: "Yellow"
};

// Check if "color" exists in dog or not.
if ("color" in dog) {
    console.log("name is in dog object");
} else {
    console.log("name is not in dog object");
}

// Check if "country" exists in dog or not.
if ("country" in dog) {
    console.log("country is in dog object");
} else {
    console.log("country is not in dog object");
}

Output

color is in dog object
country is not in dog object

Using the hasOwnProperty() method

Syntax

objectName.hasOwnProperty(prop)

Parameters

  • prop: the name of the property you need to check.
  • objectName: the name of the object in which you need to check if a key exists.

Return value

  • True if the prop exists in objectName.
  • False if the prop doesn’t exist in objectName.

Look at the example below.

const student = {
    name: "Perter",
    birth: 2002, 
    sex: "Male",
    country: "U.S",
    email: "perter@learnshareit.com",
};

let str1 = "birth";
let str2 = "address";

// Check if "birth" exists in student or not.
if (student.hasOwnProperty(str1)) {
    console.log(str1 + " is in student object");
} else {
    console.log(str1 + " is not in student object");
}

// Check if "address" exists in myO or not.
if (student.hasOwnProperty(str2)) {
    console.log(str2 + " is in student object");
} else {
    console.log(str2 + " is not in student object");
}

Output

birth is in student object
address is not in student object

Summary

You can use the in operator or the hasOwnProperty() method to check if a key exists in a JavaScript object. These are some solutions that can help you. We hope this tutorial is helpful to you. Thanks!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *