JavaScript Basic Programs for Beginners
JavaScript Basic Programs for Beginners - Important Points
26. | Which of the following is the correct way to convert a string to a number in JavaScript? |
---|
A. parseInt(string);
B. Number(string);
C. parseFloat(string);
D. All of the above
View Answer Discuss Work SpaceAnswer: option d
Explanation:
All of the options are correct ways to convert a string to a number in JavaScript.
27. | What is the output of the following code snippet? console.log("hello".indexOf("l")); |
---|
A. 1
B. 2
C. 3
D. 4
View Answer Discuss Work SpaceAnswer: option b
Explanation:
The output of the code is "2" because the indexOf() method returns the index of the first occurrence of a substring in a string.
28. | Which of the following is the correct way to round a number to the nearest integer in JavaScript? |
---|
A. Math.floor(number);
B. Math.round(number);
C. Math.ceil(number);
D. All of the above
View Answer Discuss Work SpaceAnswer: option b
Explanation:
The correct way to round a number to the nearest integer in JavaScript is by using the Math.round() method.
29. | What is the output of the following code snippet? console.log("hello".slice(1, 3)); |
---|
A. hel
B. el
C. llo
D. null
View Answer Discuss Work SpaceAnswer: option b
Explanation:
The output of the code is "el" because the slice() method returns a portion of a string between two indices.