THE WORLD'S LARGEST WEB DEVELOPER SITE

Node.js buffer lastIndexOf() Method

❮ Buffer Module


Example

Return the last occurrence of the letter "e":

var buf = Buffer.from('Hello, and welcome to Rome!');

console.log(buf.lastIndexOf('e'));
Run example »

Definition and Usage

The lastIndexOf() method checks if a specified value is present in the buffer and returns the position.

If the specified value occurs more than once, only the position of the last occurrence will be returned.

This method returns -1 if the value to search for never occurs.

See also: the indexOf() method, which returns the first occurrence of the specified value.


Syntax

buffer.lastIndexOf(value, start, encoding);

Parameter Values

Parameter Description
value Required. What to search for. Legal value types:
String
Buffer
Number (Integer)
start Optional. Where to begin the search. Default 0
encoding Optional. If the value is a string, this parameter is used to specify its encoding. Default "utf8"

Technical Details

Return Value: A Number, representing the position where the specified search value occurs for the last time, or -1 if it never occurs
Node.js Version: 6.0.0

❮ Buffer Module