JavaScript Quiz: Test Your Knowledge
JavaScript Quiz: Test Your Knowledge
In a JavaScript switch statement, 'default' is used as a fallback case that executes if none of the specified cases match the switch expression. It catches all values not specifically handled by a preceding case statement, similar to the 'else' in an if-else structure .
The 'Math.min(x, y)' method returns the smallest of zero or more numbers. It is part of the Math object and is commonly used when you need to determine the lower value between two or more numbers, such as finding the minimum in a set of values to implement logic based on thresholds .
The key difference between 'setInterval' and 'setTimeout' in JavaScript is that 'setInterval' repeatedly triggers an event at specified intervals until it is cleared, whereas 'setTimeout' triggers an event only once after a set delay. 'setInterval' is used for ongoing periodic tasks, like updating a clock display, while 'setTimeout' is suited for delayed actions, like creating a timeout after a user action .
The ternary operator '? :' in JavaScript is a shorthand for an if-else statement. In the expression '(x % 10 == 0) ? console.log("Divisible by 10") : console.log("Not divisible by 10");', it evaluates whether 'x % 10 == 0' is true. If true, it executes the first expression 'console.log("Divisible by 10")'; otherwise, it executes the second 'console.log("Not divisible by 10")'. It is useful for concise conditional expressions .
The result of the expression '3 > 2 > 1 === false' is true. The expression is evaluated from left to right, so '3 > 2' is evaluated first, returning true. In JavaScript, true is treated as 1 when compared with numbers, so '1 > 1' returns false. Thus, 'false === false' returns true as both sides are equal .
JavaScript is used as a client-side language for creating interactive web pages by manipulating the Document Object Model (DOM). It can perform tasks such as form validation, content updates, and asynchronous requests (AJAX). As a server-side language, notably with Node.js, it can handle HTTP requests, perform database operations, and serve dynamic content .
In JavaScript, 'new' is followed by a function invocation to create a new instance of an object. The function serves as a constructor. For example, 'new Point(2,3)' creates a new object with its type defined by the function 'Point'. Inside 'Point', 'this' refers to the new object. This pattern is crucial for implementing custom object types and ensuring each instance has its own properties .
JavaScript is distinguished as a 'scripting language' because it is primarily used to automate tasks and enhance interactivity within web applications, rather than for standalone software development. Unlike compiled languages, JavaScript is interpreted at runtime, which allows for dynamic execution of code within web browsers without the need for recompilation .
The method 'alert("Hello World!")' is correct for displaying a message in an alert box because 'alert' is a built-in function in JavaScript that creates a modal dialog with the specified message. Alternatives such as 'alertBox("Hello World!")' or 'msgAlert("Hello World!")' are incorrect since they are not part of JavaScript’s global methods .
In this scenario, 'var bar = new fun();' creates an instance of the function 'fun'. Inside 'fun', '++x;' increments the global variable x, and 'this.x = x;' assigns the incremented value of x (which is 1) to the property 'x' of the new object 'bar'. Therefore, 'console.log(bar.x);' outputs 1 because it accesses the 'x' property of the 'bar' object .