JavaScript is a versatile language widely used for web development. Today, we'll explore some Basics Javascript Interview Questions.




Javascript Interview Questions & Answers


1. Summing Two Numbers

Let's start with a simple function that sums two numbers:


The sum function takes two parameters, a and b, and returns their sum. The console.log statement outputs the result of sum(4, 8), which is 12.


2. Finding the Maximum Number in an Array

Solution 1 : Using Math.max and the Spread Operator





In this solution, Using Math.max with the spread operator (...). This operator expands the array into individual elements, allowing Math.max to compare them directly.


Solution 2 : Using for loop




The maxNumber function initializes max to the first element of the array and then iterates through the rest of the array. If it finds a larger number, it updates max. Finally, it returns the maximum value.



3. Reversing a String





Here, reverseString function splits the string into an array of characters, reverses the array, and then joins the characters back into a string.

4. Capitalizing the First Letter of a String





The firstLetterCapital function converts the first character of the string to uppercase and the rest of the string to lowercase. It uses charAt(0) to get the first character and slice(1) to get the remainder of the string.

charAt() : The charAt() method in JavaScript is used to return the character at a specified index in a string.


So, That's all for Today. I hope this post will help.