If you will use equal operator(=) in if condition the if block will always be executed.
You need to use == operator or === .
Example
Following is the code −
var details = [
{
id: 10001,
name: "John"
},
{
id: 10002,
name: "Bob"
},
{
id: 10003,
name: "Carol"
},
{
id: 10004,
name: "David"
}
]
var searchId = 10003;
for (var index = 0; index < details.length; index++) {
if (details[index].id === searchId) {
console.log(details[index].id + " found");
break;
}
}To run the above program, you need to use the below command −
node fileName.js.
Here, my file name is demo322.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo322.js 10003 found