Basics of JavaScript for technical interviews
Do you remember how to read from files?
Even if you use JavaScript on a daily basis, you might have forgotten some of the very basics of JavaScript that are required for some technical interviews. Let’s refresh our minds on these:
- How to read from the console
- How to process command line arguments
- How to read and write from and to files
- How to parse CSV or text files
- How to read the length of string
- How to declare and use 2D arrays
How to read from the console
To read from the console using JavaScript, you can use readline
module from Node.js.
import readline from "readline";const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});rl.question("Input data: ", (data) => {
console.log(`Data: ${data}`); rl.close();
});
And the result is like this:
Input data: apple,watermelon,lemon
Data: apple,watermelon,lemon
You can read more about it in the Node.js doc.
How to process command line arguments
In the Node.js environment, command line arguments are stored in process.argv
. So you can simply output the arguments like:
console.log(`Arguments: ${process.argv}`);
process.argv
is an array of strings and the first two elements contain the interpreter and the file path. So just to extract arguments you can use:
const argvs = process.argv.slice(2);for (const argv of argvs) {
console.log(`Argument: ${argv}`);
}
In recent versions of the Node.js, you can also import process
using import
or require
:
import process from 'node:process';
const process = require('node:process');
And the output is:
yarn run-script src/02-process-command-line-argv.ts option1=apple option2=orangeArgument: option1=apple
Argument: option2=orange
You can read more about it in the doc.
How to read and write from and to files
To process files, you can use the fs
module. Reading from a file and making a copy can be like this:
import fs from "node:fs/promises";const main = async () => {
const sampleFile = await fs.readFile("sample.csv", "utf-8"); console.log(sampleFile); await fs.writeFile("sample-copy.csv", sampleFile);
};main();
In the output, you can see the content of the sample.csv
. You can also see sample-copy.csv
is created.
These APIs have three versions: a promise version, a callback version, and a synchronous version. You can choose whatever is best for your situation.
To read more about fs, visit the doc here.
How to parse CSV or text files
To parse CSV or text files, first, you can split each line with the new line character (\n
). After that, you can split each line with a comma.
const CSV = `apple,orange,banana
pear,grape,pineapple
milk,cheese,yogurt
`;const main = async () => {
const lines = CSV.trim().split("\n"); for (const line of lines) {
const elements = line.split(","); console.log(elements);
}
};main();
And the output is:
[ 'apple', 'orange', 'banana' ]
[ 'pear', 'grape', 'pineapple' ]
[ 'milk', 'cheese', 'yogurt' ]
It is straight forward and you just need to keep in mind two things:
- Remove unwanted spaces between elements or new line characters at the end of the file.
- If the element is not a string type, you need to convert them into the type you want. For example, you can convert the element into a number using
Number(element)
.
How to split strings
How to read the length of string
How to declare and use 2D arrays
Happy coding 😎