w3resource

JavaScript: in Operator

Description

The in operator is used to check whether a given property is available on an object.

Syntax

property in object

Parameters

property: Name of the property.

object: Name of the object.

Example:

The following web document shows how the in operator is used.

HTML Code

<!doctype html><head>
<meta charset="utf-8">
<title>JavaScript in operator example</title>
<meta name="description" content="This document contains an example of JavaScript in operator" />
</head>
<script src="javascript-in-operator-example1.js"></script>
</body>
</html>

JS Code

var employeeobj = {name:"Robert",designation:"Officer",age:"34"};
if ("designation" in employeeobj)
{
console.log('Designation property is present'); 
}
else
{
console.log('Designation property is not present'); 
}

View the example in the browser

Practice the example online

See the Pen in-1 by w3resource (@w3resource) on CodePen.


See also

Conditional Operator
comma
delete
function
instanceof
new
this
typeof
void

Previous: JavaScript: function Operator
Next: JavaScript: instanceof Operator

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

Using the window.location object

JavaScript can access the current URL using the window.location object. Pretty neat, but even cooler is that this object contains certain parts of the URL as well.
Get access to the protocol/host/pathname/search/and more!

// JavaScript can access the current URL in parts. For this URL:
'https://thatsanegg.com/example/index.html?s=article'
window.location.protocol == 'https:'
window.location.host == 'thatsanegg.com'
window.location.pathname == '/example/index.html'
window.location.search == '?s=article'

Ref: https://bit.ly/2LBj213