menu

JavaScript Basic Programs for Beginners


1. Which of the following is NOT a valid way to comment in JavaScript?

// This is a comment

/* This is a comment /

# This is a comment


2. Which of the following is NOT a valid JavaScript data type?

number

string

array

tuple


3.

What is the output of the following program?

console.log(typeof "hello");

number

string

boolean

undefined


4.

What is the output of the following code snippet?

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

1

2

3

4


5.

What is the output of the following code snippet?

console.log("hello".length);

hello

5

null

undefined


6.

What is the output of the following code snippet?

console.log(3 === "3");

1

0

null

undefined


7.

What is the output of the following program?

console.log("hello".toUpperCase());

HELLO

hello

hElLo

null


8. 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)) {}


9. 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;


10. Which of the following is the correct way to create an array in JavaScript?

var myArray = ();

var myArray = [];

myArray = array();

myArray = {};