Skip to content
Related Articles

Related Articles

Improve Article

Node.js | Buffer.isEncoding() Method

  • Last Updated : 31 Dec, 2019

The Buffer.isEncoding() method checks whether the given encoding is supported or not. This method returns a Boolean value either true or false.

Syntax:

Buffer.isEncoding( encoding )

Parameters: This method accepts single parameter encoding which holds the encoding name. The supported encoding are ascii, utf8, utf16le, ucs2, base64, latin1, binary etc.

Example 1:




// Node.js program to demonstrate the 
// Buffer.isEncoding() method 
       
// Displays whether the given encoding 
// is supported or not
console.log(Buffer.isEncoding('utf8'));
console.log(Buffer.isEncoding('utf16le'));
console.log(Buffer.isEncoding('ascii'));
console.log(Buffer.isEncoding('asciivalue'));
console.log(Buffer.isEncoding('base64'));
console.log(Buffer.isEncoding('basename'));

Output:



true
true
true
false
true
false

Example 2:




// Node.js program to demonstrate the 
// Buffer.isEncoding() method 
       
// Displays whether the given encoding 
// is supported or not
console.log(Buffer.isEncoding('ucs2'));
console.log(Buffer.isEncoding('name'));
console.log(Buffer.isEncoding('binary'));
console.log(Buffer.isEncoding('latin1'));
console.log(Buffer.isEncoding('base64'));

Output:

true
false
true
true
true

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

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :