menu

JavaScript Basic Programs for Beginners


1. Which of the following is the correct way to access an element in an array in JavaScript?

myArray[elementIndex]

elementIndex.myArray

myArray[elementValue]

myArray[elementIndex][elementValue]


2. Which of the following is the correct way to check if a variable is an array in JavaScript?

if (typeof myArray === "array") {}

if (myArray.isArray()) {}

if (myArray === "Array") {}

if (Array.isPrototypeOf(myArray)) {}


3.

What is the output of the following code snippet?

console.log(5 > 2 && 2 < 4);

1

0

null

undefined


4.

What is the output of the following code snippet?

console.log("hello".slice(1, 3));

hel

el

llo

null


5. Which of the following is the correct way to round a number to the nearest integer in JavaScript?

Math.floor(number);

Math.round(number);

Math.ceil(number);

All of the above


6.

What is the output of the following code snippet?

console.log(2 * "3");

5

6

9

23


7. Which of the following is the correct way to remove an element from the end of an array in JavaScript?

myArray.removeLast();

myArray.pop();

myArray.delete(myArray.length);

myArray[myArray.length] = null;


8. Which of the following is the correct way to add an element to the end of an array in JavaScript?

myArray.push(newElement);

myArray.add(newElement);

myArray.insert(newElement, myArray.length);

myArray[myArray.length] = newElement;


9.

What is the output of the following program?

console.log(typeof "hello");

number

string

boolean

undefined


10.

What is the output of the following code snippet?

console.log("hello".indexOf("l"));

1

2

3

4