Question: How can you use arrow functions with 'map()', 'reduce()', and 'filter()' in JavaScript?
|
Answer: Arrow functions provide a concise syntax for writing function expressions. Below please find an example:
const doubledValues = array.map(value => value * 2);
const sum = array.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
const evenNumbers = array.filter(value => value % 2 === 0);
|
Question: What are jQuery selectors, and how do they work?
|
Answer: jQuery selectors are expressions used to select and manipulate HTML elements in the DOM. They work similarly to
CSS selectors, allowing one to target elements based on their tag name, class, ID, attributes, and more. For example:
$("p") // Selects all <:p> elements
$(".classname") // Selects all elements with class "classname"
$("#id") // Selects the element with ID "id"
|
Question: How do you add and remove classes with jQuery?
|
Answer: To add a class to an element, one can use '.addClass()' method, and to remove a class, you can use
'.removeClass()' method. For example:
$("selector").addClass("classname");
$("selector").removeClass("classname");
|
Question: How do you animate elements with jQuery?
|
Answer: jQuery provides several methods for animating elements, such as:
'.fadeIn()',
'.fadeOut()',
'.slideUp()',
'.slideDown()',
'.animate()', etc.
These methods allow one to change CSS properties over time to create smooth animations on the web page.
|
Question: What is the purpose of the 'filter()' function in JavaScript and how is it used?
|
Answer: The 'filter()' function is used to create a new array containing only the elements of the original array
that satisfy a provided condition or pass a provided test function. It is used as below:
array.filter(callback(element, index, array), thisArg)
• callback: Function to test each element of the array. Return true to keep the element, false otherwise.
• element: The current element being processed in the array.
• index (optional): The index of the current element being processed.
• array (optional): The array that filter() is being applied to.
• thisArg (optional): Value to use as this when executing the callback function.
|
Question: How do you handle events with jQuery?
|
Answer: One can handle events with jQuery by using the '.on()' method or by directly attaching event handler functions
to selected elements using methods like '.click()', '.hover()', '.keyup()', etc.
|