Coding Style - JavaScript Interview Questions
Code Quality: Code Style
When it comes to coding style and application development, what is the goal?
View Answer:
Technical Response: The goal of coding style and application development is to create maintainable, efficient, and readable code, ensuring optimal functionality, collaboration, and ease of understanding for developers and users alike.
Several code styling guides help programmers reach this goal.
Here's a table of some commonly used style guides in web development:
Style Guide | URL |
---|---|
Airbnb JavaScript Style Guide | Link |
Google JavaScript Style Guide | Link |
Google HTML/CSS Style Guide | Link |
Mozilla CSS Style Guide | Link |
W3C's CSS Guidelines | Link |
Idiomatic.js (Principles of JavaScript Style) | Link |
Bootstrap CSS Code Guide | Link |
StandardJS (JavaScript Style Guide, Linter, and Formatter) | Link |
Please note that you should follow the style guide recommended or required by your team, project, or organization. Also, note that the URLs above were valid as of my last training data in September 2021 and might have changed since.
Should you always use curly braces with conditional if statements?
View Answer:
// conditional if statement
if (n < 0) {
console.log(`Power ${n} is not supported`);
} else {
console.log(`Power ${n} is supported`);
}
Should code lines in JavaScript be long and horizontal or split into separate lines? Provide a justification for your decision
View Answer:
// backtick quotes (`) allow splitting the string into multiple lines
let str = `
ECMA International's TC39 is a group of JavaScript developers,
implementers, academics, and more collaborating with the community
to maintain and evolve the definition of JavaScript.
`;
// Conditional If statement variable conditions split in multiple lines
if (id === 123 && moonPhase === 'Waning Gibbous' && zodiacSign === 'Libra') {
letTheSorceryBegin();
}
A great example would be long paragraphs longer than 120 characters. We can use backticks to handle lines longer than 120 characters.
What do development teams typically agree to for the maximum character length?
View Answer:
// backtick quotes (`) allow splitting the string into multiple lines
let str = `
ECMA International's TC39 is a group of JavaScript developers,
implementers, academics, and more collaborating with the community
to maintain and evolve the definition of JavaScript.
`;
What is a best practice for vertical space of code?
View Answer:
function pow(x, n) {
let result = 1;
// <--
for (let i = 0; i < n; i++) {
result *= x;
}
// <--
return result;
}
What is one way to reduce nesting levels in your code?
View Answer:
Here are some rules of thumb for reducing nesting in your code:
- Keep your conditional blocks brief. Keeping things local improves readability.
- Think about restructuring if your loops and branches are more than two layers deep.
- Consider separating layered logic into distinct functions. For example, you may write a function to handle each item instead of using a double nested loop to cycle through a list of objects containing a list (such as a protocol buffer with repeated fields).
function pow(x, n) {
if (n < 0) {
console.log("Negative 'n' not supported");
} else {
let result = 1;
for (let i = 0; i < n; i++) {
result *= x;
}
return result;
}
}
function pow(x, n) {
if (n < 0) {
console.log("Negative 'n' not supported");
return;
}
let result = 1;
for (let i = 0; i < n; i++) {
result *= x;
}
return result;
}
What is the purpose of a linter in JavaScript?
View Answer:
The great thing about linters is that style-checking can also find bugs, like typos in variable or function names. Because of this feature, using a linter is recommended even if you do not want to stick to one particular code style.