• Node.js Video Tutorials

Node.js - Buffer.isEncoding() Method



The NodeJS Buffer.isEncoding() method returns the true if the given encoding value is a supported character encoding and false if not.

Syntax

Following is the syntax of the NodeJS Buffer.isEncoding() Method

Buffer.isEncoding( encoding )

Parameters

  • encoding − (required) The character encoding value that needs to be checked.

Return value

The method Buffer.isEncoding() returns a boolean value true/false.It returns true if the given encoding is a valid encoding supported and false if not.

Example

In this example I will test the utf-8, hex encoding and see the output of Buffer.isEncoding() method.

const utf8encoding = Buffer.isEncoding('utf-8');
const hexencoding = Buffer.isEncoding('hex');
console.log("Result for utf-8 encoding is : "+ utf8encoding);
console.log("Result for hex encoding is : "+ hexencoding);

Output

Since both utf-8 and hex are proper character encoding the output will be true.

Result for utf-8 encoding is : true
Result for hex encoding is : true

Example

In this example will test a simple string value which is not an encoding character..

const helloencoding = Buffer.isEncoding('hello');
console.log("Result for hello encoding is : "+ helloencoding);

Output

Since "hello" is invalid encoding value the output will be false from Buffer.isEncoding() method. On executing the above program, it will generate the following output −

Result for hello encoding is : false
nodejs_buffer_module.htm
Advertisements