Buffer — binary data. String — text data.
Buffer:
1// Create buffer2const buf = Buffer.from("Hello", "utf-8");3console.log(buf); // <Buffer 48 65 6c 6c 6f>45// From file6const fs = require("fs");7const data = fs.readFileSync("file.txt");
String:
1const str = "Hello";2console.log(str); // Hello
Conversion:
1// String to Buffer2const buf = Buffer.from("Hello", "utf-8");34// Buffer to String5const str = buf.toString("utf-8");
When to use: