JavaScript MCQs

JavaScript MCQs

Answer these 1000+ JavaScript MCQs and assess your grip on the subject of JavaScript. Scroll below and get started!

1: JavaScript is ...

A.   subjective

B.   evil

C.   object based

D.   objective

2: Math.PI returns the mathematical constant π. What standard JavaScript method would return "3.14"?

A.   Math.PI.toString("D2")

B.   Math.PI.toFixed(2)

C.   Math.Round(Math.PI)

D.   Math.PI.toPrecision(2)

3: What is the result? 0 == ""

A.   Error: type mismatch

B.   true

C.   false

4: How do you assign object properties?

A.   obj["age"] = 25 OR obj.age = 25

B.   obj.age = 25 OR obj(@"age") = 25

C.   obj(age) = 25 OR obj.age = 25

5: What is the result of the following statement: 0 == "";

A.   true

B.   null

C.   false

D.   Throws Error, invalid comparison

6: Math.random() returns..

A.   a random number that can be any value

B.   a random number between 0 and 100

C.   a random number between 0 and 1

D.   a random number between 0 and 1000

7: var x = "foo"; x = !!x; What is the value of x?

A.   true

B.   undefined

C.   NaN

D.   "!!foo"

8: How do you create an object in JavaScript?

A.   var obj = new Object();

B.   var obj = {};

C.   function Foo() {} var obj = new Foo();

D.   All of these work.

9: When writing an object literal, what is used to separate the properties from each other?

A.   an underscore "_"

B.   a colon ":"

C.   a full-stop "."

D.   a semicolon ";"

E.   a comma ","

10: What is the value of x? var a = false; var x = a ? “A” : “B”;

A.   false

B.   true

C.   "B"

D.   "A"

E.   undefined

11: Which of these is not a logical operator?

A.   &

B.   &&

C.   ||

D.   !

12: How do you find the number with the highest value of x and y?

A.   Math.ceil(x, y)

B.   max(x, y)

C.   top(x, y)

D.   Math.max(x, y)

E.   ceil(x, y)

13: Which of the following asserts that the variables `A`, `B`, and `C` have unequal values?

A.   A !== B

B.   A !== B && B !== C && A !== C

C.   A !== B & B !== C

D.   A !== B || B !== C

14: What is the difference between a while loop and a do...while loop?

A.   The code inside a while loop will always be executed at least once, even if the condition is false.

B.   The code inside a do...while loop will always be executed at least once, even if the condition is false.

C.   There is no difference between them.

15: How to return the first value of this array? var myArr = [1, 2, 3, 4, 5]; var myVal = ...

A.   myArr.unshift();

B.   myArr[0];

C.   myArr.shift();

D.   myArr[1];

E.   myArr.pop();

16: Which is NOT a way to create a loop in javascript?

A.   while (...) { }

B.   do { } while(...)

C.   for (...) { }

D.   repeat (...) { }

17: What is the correct JavaScript syntax to insert a comment that can span multiple lines?

A.   / This comment has more than one line /

B.   // This comment has more than one line //

C.   /* This comment has more than one line */

D.   // This comment has mor than one line *//

18: Where do you use the "break" statement?

A.   To add a value to an array.

B.   To delete a (global) variable.

C.   To terminate an Object statement.

D.   To divide (or "break") a mathematical value in half.

E.   To terminate a switch statement, loop, or labeled block.

19: In an array object, what is the key of the first value?

A.   100

B.   $

C.   0

D.   -1

E.   1

20: what is the difference between x++ and ++x?

A.   ++x is depreciated and replaced with x++;

B.   x++ will return the value of x and then increment, where as ++x will increment the variable first then return its value.

C.   They both do the same thing, just different syntax

21: The var statement is used to:

A.   Declare a member of a class

B.   Change a constant

C.   Create a new local variable

D.   Retrieve a variable descriptor

22: Which of the following primitive values exist in JavaScript?

A.   number

B.   All of these

C.   string

D.   boolean

23: Which statement loops through an array?

A.   for (var i=0; i < myArray.length; i++)

B.   for (i = 0; i <= myArray.length;)

C.   for (i < myArray.length; i++)

24: You use the Math.pow() method to:

A.   Return any number

B.   Return a number raised to the power of a second number

C.   Return a random value between 0 and 1

D.   Return a variable value

25: What is the value of ("dog".length)?

A.   2

B.   3

C.   4

26: How do you round the number 7.25, to the nearest whole number?

A.   Math.round(7.25)

B.   round(7.25)

C.   Math.rnd(7.25)

D.   rnd(7.25)

27: Are variable identifiers case-sensitive?

A.   Yes

B.   No

28: String literals are written using:

A.   Just double quotes: "example"

B.   Just single quotes: 'example'

C.   Either double quotes or single quotes: "example" and 'example'

29: Which of the following is a valid function definition?

A.   function myFunc(arg1, arg2):

B.   func myFunc = (arg1 as string, arg2 as int) { }

C.   function myFunc(arg1,arg2) { }

30: JavaScript supports dynamic typing, you can assign different types of values to the same variable.

A.   true

B.   false

31: How do you check what the type of a value in variable x is?

A.   x.__type;

B.   typeof(x);

C.   Object.type(x);

D.   gettype(x);

32: What does the "break" statement do?

A.   Cancels the current event.

B.   Aborts the current loop or switch statement.

C.   Aborts the current function.

D.   Simulates a JavaScript crash.

33: Given the following code, what does myFunc() return? var foo = 'foo'; var bar = 'bar'; function myFunc() { return foo + bar; }

A.   An error is thrown because of illegal out of scope access.

B.   "foo + bar"

C.   NaN

D.   "foobar"

E.   "undefinedundefined"

34: Which is the correct way to write a JavaScript array?

A.   var names = array("Tim", "Kim", "Jim");

B.   var names = {1: "Tim", 2:"Kim", 3:"Jim"};

C.   var names = {0: "Tim", 1: "Kim", 2: "Jim"};

D.   var names = ["Tim","Kim","Jim"];

35: Which of the following is a JavaScript comment?

A.   // comment

B.   \\ comment

C.  

D.   -- comment

E.   # comment

36: How do you define a function called "fName"?

A.   new fName = { }

B.   func fName = function () {}

C.   None of these

D.   function fName() { }

E.   function fName: { }

37: Which symbol is not used in logical operations?

A.   ||

B.   &&

C.   %

D.   !

38: Which of the following declares a variable with a value of string type?

A.   string myVar = "This is a string";

B.   var myVar = "This is a string";

C.   var string myVar = "This is a string";

39: Which of the following is not a reserved word?

A.   throw

B.   program

C.   void

D.   return

40: Which of these could be a correct way to create an instance of Person?

A.   new john = Person('John', 'Doe', 50, 'blue');

B.   var Person john = new Person('John', 'Doe', 50, 'blue');

C.   var john = new Person('John', 'Doe', 50, 'blue');

D.   Person john = new Person('John', 'Doe', 50, 'blue');

41: Which of these will throw a SyntaxError?

A.   if (x = 1) { }

B.   if (x ==== 1) { }

C.   if (x === 1) { }

D.   if (x == 1) { }

42: What is the value of the following expression: 8 % 3

A.   5

B.   2

C.   24

D.   Other/Error

43: What operator is used for string concatenation?

A.   +

B.   &

C.   All of these

D.   .

44: Which of these descriptors applies to JavaScript?

A.   Strongly typed, variables are declared with a type, and you can not assign another type to the variable.

B.   Loosely typed, values of any type can be assigned to any variable.

45: Properties of objects may be accessed using...

A.   none of these

B.   the dot notation in JavaScript.

C.   the redirect notation in JavaScript.

46: Properties of a RegExp object include:

A.   lastIndex

B.   source

C.   All of these

D.   ignoreCase

47: What does isNaN() do?

A.   Only returns true if the argument is not a number

B.   Converts a non-numeric value to a number.

C.   Throws an error if a conditional statement is false.

48: The `else` statement is ___

A.   used inside of an `if` statement. To specify the code that should execute if the `if` condition is no longer true.

B.   used together with the `if` statement to specify the code that should execute when the `if` condition is false.

C.   Does not exist, in JavaScript `or` and `then` are used to specify code to execute for the "false" case of the `if` statement.

49: USERNAME and userName

A.   Represent the name of different variables

B.   Represent the name of the same constant

C.   Represent the name of the same variable

D.   Represent the name of different constants

50: What is JavaScript used for?

A.   Manipulating Web Page Behaviors

B.   Styling web pages

C.   A website cannot be created without JavaScript

D.   None of the above.

E.   Content definition within web pages