Node.js is a popular tool in software development. What is its main use?
Think about where JavaScript usually runs and what Node.js changes about that.
Node.js allows JavaScript to run outside the browser, mainly on servers, enabling backend development.
Consider this Node.js code snippet. What will it print to the console?
console.log(typeof process);
Think about what process is in Node.js.
The process is a global object in Node.js representing the current process, so its type is "object".
Node.js supports different module systems. Which code correctly imports the built-in 'fs' module using ES module syntax?
ES modules use import statements. Think about how to import all exports as one object.
In ES modules, import * as fs from 'fs'; imports the entire module as an object named fs.
Look at this Node.js code using setTimeout. What will it print?
console.log('Start'); setTimeout(() => console.log('Inside timeout'), 0); console.log('End');
Remember how JavaScript handles asynchronous callbacks with the event loop.
Even with zero delay, setTimeout callbacks run after the current code finishes, so 'End' prints before 'Inside timeout'.
Given this code snippet, which option will cause a runtime error when executed?
import * as fs from 'fs'; fs.readFile('nonexistent.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); });
Think about what happens when you try to read a file that does not exist.
Trying to read a missing file causes an error passed to the callback. Throwing that error crashes the program.