Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
How to Check an Element with Specific ID Exists using JavaScript ?
Next article icon

How to search an element without using any loops in Node.js ?

Last Updated : 09 Sep, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

The setInterval() method repeats or re-schedules the given function at every given time-interval. It is somewhat like window.setInterval() Method of JavaScript API, however, a string of code can’t be passed to get it executed.

Syntax:

setInterval(timerFunction, millisecondsTime);  

Parameter: It accepts two parameters which are mentioned above and described below:

  • timerFunction <function>: It is the function to be executed.
  • millisecondsTime <Time>: It indicates a period of time between each execution.

The clearInterval() method is used to stop the next schedule code execution. It is somewhat like window.clearInterval() Method of JavaScript API, however a string of code can’t be passed to get it executed.

Syntax:

clearInterval(intervalVar);  

Parameter: It accepts a single parameter which is mentioned above and described below:

  • intervalVar <function>: It is the function name that is needed to stop executing for next interval.

Examples:

  Input: Array = [ 46, 55, 2, 100, 0, 500 ]  Search Element = 0    Output: Element 0 found at index 4      Input: Array = [8, 9, 2, 7, 18, 5, 25]  Search Element = 500    Output: Element 500 not found in Array.  

Approach: The sorting requires visiting each element and then performing some operations, which requires for loop to visit those elements.

Now here, we can use setInterval() method to visit all those elements and perform those operations.

The below code illustrates the above approach in JavaScript Language.

Example 1: File Name: Index.js




// Node.js program to search an element 
// without using any loops provided Array
  
const arr = [46, 55, 2, 100, 0, 500];
const l = arr.length;
var j = 0;
  
// Element to Search
var srchElement = 0;
  
// setInterval for looping purpose 
var myVar1 = setInterval(myTimer1, 1);
  
function myTimer1() {
    if (arr[j] == srchElement) {
  
        // Clear interval as required 
        // element is found 
        clearInterval(myVar1);
  
        // Printing found element
        console.log("Element", srchElement, 
            "found at index", arr.indexOf(arr[j]));
    }
  
    j++;
  
    if (j == l) {
        // Clear interval as element
        // not found in array
        clearInterval(myVar1);
  
        // Printing that element not found
        console.log("Element", srchElement, 
                    "not found in Array.");
    }
} 
 
 

Run index.js file either on the online compiler or follow the following:

node index.js

Output:

  

Element 0 found at index 4

Example 2: File Name: index.js




// Node.js program to search an element 
// without using any loops provided Array
  
const arr = [8, 9, 2, 7, 18, 5, 25];
const l = arr.length;
var j = 0;
  
// Element to Search
var srchElement = 50;
  
// setInterval for looping purpose 
var myVar1 = setInterval(myTimer1, 1);
  
function myTimer1() {
  
    if (arr[j] == srchElement) {
          
        // Clear interval as required
        // element is found 
        clearInterval(myVar1);
  
        // Printing found element
        console.log("Element", srchElement, 
            "found at index", arr.indexOf(arr[j]));
    }
  
    j++;
  
    if (j == l) {
  
        // clear interval as element not
        // found in array
        clearInterval(myVar1);
  
        // Printing that element not found
        console.log("Element", srchElement, 
                "not found in Array.");
    }
} 
 
 

Run index.js file either on the online compiler or follow the following:

node index.js  

Output:

  

Element 50 not found in Array.



Next Article
How to Check an Element with Specific ID Exists using JavaScript ?
author
amitkumarjee
Improve
Article Tags :
  • JavaScript
  • Node.js
  • Web Technologies
  • Node.js-Misc

Similar Reads

  • How to loop through HTML elements without using forEach() loop in JavaScript ?
    In this article, we will learn how to loop through HTML elements without using the forEach() method. This can be done in the following ways: Table of Content Approach 1: Using the for loopApproach 2: Using the While loopApproach 3: Using the 'for.....of' statementApproach 4: Using the for...in state
    4 min read
  • How to Make a search function using Node Express and MYSQL
    In this article, we will learn how to create a search function using Node with Express framework to search terms inside MYSQL tables. Prerequisites:MySQLNode JSExpress JSWe will discuss the following methods to search: Table of Content Searching term with partial matchSearching term with exact match
    5 min read
  • How to Perform a Find Operation with Limit and Skip in MongoDB using Node.js?
    In MongoDB, the find operation is used to query the database and retrieve documents that match a specified criterion. Using limit and skip along with find allows for efficient pagination of results. limit specifies the maximum number of documents to return, while skip specifies the number of documen
    3 min read
  • How to add Search Feature in Next.js using Algolia ?
    Adding a search feature to your Next.js application can greatly enhance user experience by providing fast and relevant search results. Algolia is a powerful search-as-a-service solution that integrates seamlessly with Next.js to offer instant, full-text search capabilities. In this article, we will
    3 min read
  • How to Check an Element with Specific ID Exists using JavaScript ?
    Given an HTML document containing some elements and the elements contain some id attribute. The task is to check whether the element with a specific ID exists or not using JavaScript. Below are the approaches to check an element with specific ID exists or not using JavaScript:  Table of Content Appr
    3 min read
  • How to replace one document in MongoDB using Node.js ?
    MongoDB, the most popular NoSQL database, we can count the number of documents in MongoDB Collection using the MongoDB collection.countDocuments() function. The mongodb module is used for connecting the MongoDB database as well as used for manipulating the collections and databases in MongoDB. Insta
    1 min read
  • How to Implement Search and Filtering in a REST API with Node.js and Express.js ?
    Search and filtering are very basic features that an API must possess to serve data to the client application efficiently. By handling these operations on the server-side, we can reduce the amount of processing that has to be done on the client application, thereby increasing its performance. In thi
    5 min read
  • How To Perform a Find Operation With Sorting In MongoDB Using Node.js?
    Performing a find operation with sorting in MongoDB using Node.js is a common task for developers working with databases. This guide will walk you through the process step-by-step, including setting up a MongoDB database, connecting to it using Node.js, performing a find operation, and sorting the r
    3 min read
  • How to Perform Text Search in MongoDB using Node.js?
    MongoDB is an open-source, cross-platform, No-SQL database that stores data in documents, which contain data in the form of key-value pairs. In this article, we will learn about how to perform text-based searches in MongoDB using node.js. Prerequisites Node.jsMongoDBMongoDB Atlas Connect with Applic
    5 min read
  • How to Perform a findOne Operation in MongoDB using Node.js?
    The findOne operation in MongoDB is used to get a single document from the collection if the given query matches the collection record. While using findOne, if more than one record is there with the exact same match, then it will return the very first one. We will use this operation if we need to fe
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences