Node.js | Buffer.includes() Method

Buffer is a temporary memory storage which stores the data when it is being moved from one place to another. It is like an array of integers.

The Buffer.includes() method checks whether the provided value is present or included in the buffer or not.

Syntax:



buffer.includes( value, byteOffset, encoding );

Parameters: This method accept three parameters which are listed below:

  • value: It holds the value that you want to find in buffer.
  • byteOffset: It is optional parameter. It refers to the starting index from which the elements of input buffer will be searched. The default value is 0.
  • encoding: If the needed value is string, then you can add then encoding type also. The default value is utf-8.

Return Value: This method returns a Boolean value either True or False depending on the value. If value found in buffer then return True otherwise return False.

Below examples illustrate the use of Buffer.includes() Method in Node.js:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the  
// Buffer.includes() Method 
      
// Creating a buffer
const buffer = new Buffer('Geek One');
  
console.log(buffer.includes('Geek'));

chevron_right


Output:

true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node program to demonstrate the  
// Buffer.includes() Method 
      
const buffer = Buffer.from(
        'GeeksforGeeks: A computer science portal');
  
// Started checking the value from index 15 only
const output = buffer.includes('Geek', 15);
  
console.log(output);

chevron_right


Output:

false

Note: The above program will compile and run by using the node index.js command.

Reference: https://nodejs.org/api/buffer.html#buffer_buf_includes_value_byteoffset_encoding



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.