Top 3 JavaScript Tricky Basic Interview questions and answers[PART III]
Are you currently looking for a new job? Those interview questions look simple but you are not able to answer? How to nail the technical interview? Today we will go through those basic technical interview questions which you MUST know before attending any interview. Please click PART I and PART II if you have not read them yet.
1. Avoid if-else hell in JavaScript
// skip variable declaration for variant and color
if (variant === 'error') color = 'red';
else if (variant === 'success') color = 'green';
When candidates see the code block above, it is considered as a red flag if they are not able propose a better way to avoid the if-else hell in JavaScript or they do not think it is a MUST to avoid it.
Well, the best approach is to create an object and map it with the corresponding value.
const mappedVariantToColor= {
error: 'red',
success: 'green',
defaultVariant: 'blue',
warining: 'yellow'
};
2. Avoid repeated code in JavaScript
There are tons of developers who have their own coding styles and habits. However DRY(don’t repeat yourself) is the master principle of coding.