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:
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
.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.
reverseString
function splits the string into an array of characters, reverses the array, and then joins the characters back into a string.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()
method in JavaScript is used to return the character at a specified index in a string.
0 Comments