- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
I'm trying to make an id searcher which does a thing when you input the right id. However, the JavaScript if statement always runs. How?
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
Advertisements