<<

1) When the

element in the code is clicked, which element's onclick event will run first?

Form
Div


Paragraph

The onclick event on the

element will be triggered first. This is called event bubbling. An event bubbles from the innermost element to the outermost element in the DOM hierarchy.

2) When the

element is clicked, which element in the code captures the event first?

Form
Div


Paragraph

The onlick event is first captured by the

element. Event capturing starts at the highest level in the DOM hierarchy tree (generally, the Window object).

3) When does the load event occur?

The load event occurs when a document (page) finishes loading in the browser. When the submit button in a form is clicked, the submit event is triggered.

4) Which of the following functions is created at runtime and declared without a name?

Anonymous functions are declared without a name. When a page is rendered, the function definitions are hoisted and all functions are created at runtime.

5) Which JavaScript property is used to add new properties and methods to objects?

The prototype property is used to add new properties and methods to objects. New methods and functions are attached to the prototype property of the object, which are then automatically added to the object itself. 6) Which of the following expressions is an equivalent of the sample() function? function sample(a, b) { a++; b++; return a + b; }

The following expression represents an arrow function: var sample = (m, n) => ++m + ++n;

Other expressions are either syntactically incorrect or will return incorrect values of variables. In the given code block, the value of the variables a and b is incremented before returning. The pre-increment operators modify the value of the variables before using them.

The following expression will not increment the value before returning: var sample = function (m, n) { return m++ + n++; }

The following expressions are syntactically incorrect: var sample => (m, n) = { return ++m + ++n; } function sample = (m, n) { return ++m + ++n; }

7) How does a global variable differ from a local variable?

Global variables can be accessed throughout the page, in any function of code block.

8) What is the technique of changing the datatype of a variable called in JavaScript?

The datatype of a variable can be changed in JavaScript. This technique is called casting. Hoisting is the of moving variable declarations to the top when interpreting the code. Parsing is the process of analyzing the code. 9) What will be the output of the following code snippet? var x = 10, y = "10.1x", z = "15xy"; var a = x + parseInt(y) + parseFloat(z); document.write(a);

The parseInt(y) method returns 10, and the parseFloat(z) method returns 15. Now all variables in the expression are numbers, var a = x + 10 + 15; // equals 35

Hence, the output will be 35.

10) Which code statement should be added to the function to return to the calling statement the value of x multiplied by 2? function myFunction(x) { //your code here }

The statement that should be added is return x * 2. JavaScript functions can return values to the calling statement using the return statement. The value to be returned is placed after the return keyword. The value that is returned from a function can be assigned to a variable or it can be used in an expression.

11) Which keyword points to the current owner of a function or an object?

The this keyword points to the current owner of a function, an object, a variable, code, etc. You can also use it explicitly to specify the owner (or parent).

12) What does the term loosely typed language mean?

JavaScript is a loosely typed language, which means that you don't need to declare the type of a variable before using it. Also, you can change the datatype of a variable as you want throughout the script. 13) What will be the output of this code block? function sample(a, b) { a++; b++; } var a = 10, b = 15; sample(a, b); document.write(a + b);

The expression var a = 10, b = 15; assigns the values 10 and 15 to variables a and b, respectively. When the sample() function is called, the values of local variables (variables local to the sample() function block) a and b, inside the function, is incremented by 1 each. This increment does not affect the global variables a and b. Finally, the values passed to the document.write() method are both numbers, hence the output is 25.

14) What will be the output of this code snippet? function example() { const x = 10; return x; } var x = 15; document.write(example());

Since, the constant x is locally declared, it will not cause a syntax error. The function will return the value 10, which is assigned to the variable x inside the example() function. Hence, the output will be 10.

15) What will be the output of the code, if a user enters "Adam" and "Warlock" at the two prompts? function sample() { return fname + lname; } var fname = prompt("Please enter your first name:"); var lname = prompt("Please enter your last name:"); document.write(sample());

The variables defined using var, fname and lname, have global scope. Hence, the sample() function will have no problem in accessing them. And the variables fname and lname have been assigned the values "Adam" and "Warlock", respectively. So, the output of the code snippet will be "AdamWarlock." 16) Which of the following statements is true of JavaScript keywords and/or reserved words?

Reserved words have special meaning for the interpreter and they cannot be used as identifiers or names for objects.

17) What are JavaScript keywords?

Keywords are special words in JavaScript that cannot be used as identifiers. They possess a special meaning for the parser, and their syntax rules must be followed when using them.

18) Assume that the user inputs the numbers 2 and 3 when the script is run in the browser. What is a possible result from this script? firstNumber = prompt("Enter a number" , ""); secondNumber = prompt("Enter another number" , ""); alert("The sum of your two numbers is " + (firstNumber + secondNumber));

The prompt() method returns a string, unless specifically instructed otherwise, so these numbers will most likely be cast as strings instead of numerals, and the + operator will concatenate them (2+3=23) instead of adding them (2+3=5).

19) What is a possible result from this code when it is run in the browser? weather = confirm("Click OK if it is raining" ); alert("I looked outside and the weather report is " + weather);

The confirm() method returns only two possible values: true or false.

20) Which of the following statements is true of semicolons in JavaScript?

Semicolons are used to separate statements in a script. Their use is optional when there is only one statement in a single line of code. However, if a single line contains more than one statements, they must be separated using a semicolon.