What is Buffer.isEncoding() in Node.js?

The Buffer.isEncoding() function tests if an encoding format is supported or not.

Prototype

Parameter and return value

The Buffer.isEncoding() function accepts the encoding format as a single parameter of type string.

The Buffer.isEncoding() function returns true if the encoding format is supported; otherwise, it returns false.

Examples

The following code demonstrates how to use the Buffer.isEncoding() function in Node.js.

console.log(Buffer.isEncoding('base64'));
console.log(Buffer.isEncoding('hex'));
console.log(Buffer.isEncoding('utf8'));
console.log(Buffer.isEncoding('ucs2'));
console.log(Buffer.isEncoding('binary'));
console.log(Buffer.isEncoding('utf/8'));
console.log(Buffer.isEncoding('randomencoding'));

The above example illustrates that some of the supported encoding formats are base64, hex, utf8, ucs2, and binary.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved