import fs from 'fs'; const data = fs.readFileSync('example.txt', 'utf8'); console.log(typeof data);
When you specify 'utf8' encoding in readFileSync, the returned data is a string.
Without encoding, it returns a Buffer object.
import fs from 'fs'; try { const content = fs.readFileSync('missing.txt', 'utf8'); console.log(content); } catch (err) { console.log('Error caught'); }
readFileSync throws an error if the file is not found.
The try...catch block catches this error and prints 'Error caught'.
Options A and C use the correct encoding 'utf8' which is accepted by Node.js.
Options B and D use 'utf-8' with a dash, which is not recognized and causes an error.
import fs from 'fs'; const content = fs.readFileSync('file.txt'); console.log(content.toString('utf8'));
Without specifying encoding, readFileSync returns a Buffer.
Buffer's toString method accepts an encoding argument, so this code works without error.
Synchronous operations block the main thread in Node.js, stopping all other JavaScript execution until they finish.
This means the event loop is paused during synchronous file reads.
