1. What is the correct syntax for referring to an external script called "xxx.js"?
Answer:
Explanation:
The correct syntax for referring to an external JavaScript file is <script src="xxx.js">.
2. How do you write a conditional statement for executing some statements only if "i" is NOT equal to 5?
Answer:
Explanation:
In JavaScript, '!=' is used to test if a variable is not equal to a value. The correct syntax is if (i != 5).
3. How does a 'for' loop start?
Answer:
Explanation:
The correct syntax of a 'for' loop in JavaScript is for (initialization; condition; increment).
4. What is the correct way to write a JavaScript array?
Answer:
Explanation:
In JavaScript, arrays are written with square brackets, and array elements are separated by commas.
5. Which event occurs when the user clicks on an HTML element?
Answer:
Explanation:
The onclick event occurs when the user clicks on an HTML element.
6. How do you declare a JavaScript variable?
Answer:
Explanation:
In JavaScript, a variable is declared using the 'var' keyword followed by the variable name.
7. What will the following code return: Boolean(10 > 9)?
Answer:
Explanation:
The expression 10 > 9 evaluates to true, and the Boolean() function converts it to a Boolean value, which is true.
8. Which operator is used to assign a value to a variable?
Answer:
Explanation:
The '=' operator is used in JavaScript to assign values to variables.
9. What is the correct syntax for adding comments in JavaScript?
Answer:
Explanation:
In JavaScript, single-line comments are added with two forward slashes (//), and multi-line comments start with /* and end with */.
10. How do you create a function in JavaScript?
Answer:
Explanation:
In JavaScript, a function is defined using the 'function' keyword, followed by a name, followed by parentheses ().
11. Which method is used to round a number to the nearest integer in JavaScript?
Answer:
Explanation:
Math.round() is used to round a number to its nearest integer in JavaScript.
12. How can you detect the client's browser name in JavaScript?
Answer:
Explanation:
navigator.appName returns the name of the browser the client is using.
13. Which event is fired when a form element loses focus?
Answer:
Explanation:
The onblur event is fired when an element loses focus.
14. What method is used to parse a string to an int in JavaScript?
Answer:
Explanation:
parseInt() function parses a string argument and returns an integer of the specified radix or base.
15. What will be the output of the following code: console.log(typeof "John");
Answer:
Explanation:
The typeof operator in JavaScript returns the type of a variable or an expression. In this case, "John" is a string, so it returns 'string'.
16. How do you find the number with the highest value of x and y?
Answer:
Explanation:
Math.max(x, y) is used to find the highest value between x and y in JavaScript.
17. What is the correct JavaScript syntax for opening a new window called "w2"?
Answer:
Explanation:
window.open() method is used to open a new browser window. The syntax w2 = window.open("http://www.example.com"); opens a new window with the specified URL.
18. JavaScript is the same as Java.
Answer:
Explanation:
JavaScript and Java are entirely different languages, both in concept and design. JavaScript is a scripting language used mainly for enhancing web pages and user interfaces, while Java is a programming language used for building more complex, standalone applications.
19. How do you declare a JavaScript array?
Answer:
Explanation:
JavaScript arrays are declared with square brackets and the elements are separated by commas.
20. How can you add a comment in a JavaScript?
Answer:
Explanation:
In JavaScript, single-line comments are made with two forward slashes (//), and multi-line comments are made with /* to start and */ to end.
21. What method is used to remove the last element from an array and return that element in JavaScript?
Answer:
Explanation:
The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
22. Which of the following is correct about features of JavaScript?
Answer:
Explanation:
JavaScript is indeed a lightweight, interpreted programming language that allows scripting on web pages, enhancing interactivity and functionality.
23. How can you detect if a variable is an array in JavaScript?
Answer:
Explanation:
Array.isArray() is a static method of Array that returns true if a variable is an array, otherwise false.
24. What will the following code return: Boolean(null);
Answer:
Explanation:
Boolean(null) will return false, as null is a falsy value in JavaScript.
25. What is the purpose of the 'this' keyword in JavaScript?
Answer:
Explanation:
In JavaScript, 'this' is a reference to the current object, the object that is currently being executed or used.
26. What does the 'isNaN' function do in JavaScript?
Answer:
Explanation:
The 'isNaN' function in JavaScript is used to determine whether a value is NaN (Not-a-Number) or not.
27. Which method is used to combine two or more arrays in JavaScript?
Answer:
Explanation:
The concat() method is used to merge two or more arrays. This method does not change the existing arrays but returns a new array.
28. How do you find the element with an id of "demo" in JavaScript?
Answer:
Explanation:
Both getElementById('demo') and querySelector('#demo') can be used to find an element with the id of "demo" in JavaScript.
29. What is the default behavior of a button inside a form element?
Answer:
Explanation:
By default, a button inside a form element submits the form.
30. What does the 'addEventListener' method do in JavaScript?
Answer:
Explanation:
The addEventListener() method attaches an event handler to the specified element without overwriting existing event handlers.
31. Which JavaScript operator returns true if the two compared values are not equal?
Answer:
Explanation:
In JavaScript, the '!=' operator is used to check if two values are not equal. It returns true if the operands are not equal.
32. How do you declare a JavaScript function named "myFunction"?
Answer:
Explanation:
A JavaScript function is declared with the 'function' keyword, followed by the name of the function and parentheses. The correct declaration is function myFunction().
33. What will the following JavaScript code return? console.log("Hello World".length);
Answer:
Explanation:
The 'length' property in JavaScript is used to find the length of a string. "Hello World" has 11 characters, including the space.
34. Which JavaScript method can be used to replace parts of a string?
Answer:
Explanation:
The replace() method is used in JavaScript to replace parts of a string with another string.
35. What is the correct syntax for referring to an external script called "app.js" with a relative path?
Answer:
Explanation:
The correct syntax for including an external JavaScript file is using the <script src="app.js"> tag.
36. How do you write an IF statement in JavaScript to execute some code if "x" is NOT equal to 8?
Answer:
Explanation:
In JavaScript, the correct syntax for an IF statement checking if "x" is not equal to 8 is if (x != 8).
37. Which JavaScript method is used for parsing a string to a floating point number?
Answer:
Explanation:
The parseFloat() method parses a string and returns a floating point number. It's the correct method to use for converting strings to floats.
38. What is the purpose of JavaScript's 'console.log()' method?
Answer:
Explanation:
console.log() is used in JavaScript to output a message to the web browser's console. It is commonly used for debugging purposes.
39. How do you create an object in JavaScript?
Answer:
Explanation:
In JavaScript, an object can be created using the new keyword followed by Object(). The syntax is var obj = new Object().
40. What will the following code output? console.log(typeof 42);
Answer:
Explanation:
In JavaScript, typeof is an operator that returns the data type of its operand. For the number 42, it will return 'number'.