Showing posts with label filter. Show all posts
Showing posts with label filter. Show all posts

Thursday, September 19, 2024

Anonymous Functions in JavaScript

 


1. Anonymous Functions

Anonymous functions are functions defined without a name. In JavaScript, anonymous functions are powerful features and can be used in several contexts. It is common in JavaScript to find an anonymous function assigned to a variable, passed as an argument to another function, declared to handle an event, or a callback to array methods such as map, filter, etc.


2. Benefits of Using Anonymous Functions

Anonymous functions have more concise syntax and are often more readable, especially after launching arrow functions.


3. Contexts of Use of Anonymous Functions

The following tables show the main contexts in which anonymous functions are used:


Usage of Anonymous Function

Code Snippet

Code Output

Assigned to a Variable

let sayHello = function() {

console.log("Hello, world!");

};

sayHello();


let sum = function(a, b) {

return a+b;

};

console.log("Result of addition is:", sum(4, 7));


Hello, world!

11

Argument to Another Function

setTimeout(function() {

console.log("You waited two seconds to get this message.");

}, 2000);


You waited two seconds to get this message.

Immediately Invoked Function Expression (IIFE)

(function() {

console.log("You get this message immediately.");

})();


You get this message immediately.

Callback in Event Handlers

<button id="button1">Mouse over me!</button>

<script>

document.getElementById("button1").addEventListener("mouseover", function() {

console.log("Thanks for hovering over me!");

});

</script>


Thanks for hovering over me!

In Array Methods (filter, map, etc)

let numbers = [10, 12, 345, 63, 15];

let evenNumbers = numbers.filter(function(num) {

return num % 2 === 0;

});

console.log(evenNumbers);


[10, 12]

Arrow Function Expression

let nExp = (a, n) => a ** n;

console.log(nExp(5, 4));


625


Wednesday, September 18, 2024

Main Array Functions in JavaScript

 


Here is a summary of some major array functions with examples in JavaScript.


Code snippet to define an example of an array

var persons = new Array(); var object = { name: "Mary", age:22, email: "mary@gmail.com"}; persons.push(object); object = { name: "Jack", age:25, email: "jack@gmail.com"}; persons.push(object); object = { name: "Bob", age:37, email: "bob@gmail.com"}; persons.push(object);


Function

Definition

Result Type

Code Snippet

Code Output

filter

Gets the elements satisfying the testing function

Array

const filtered_persons =

persons.filter(p => p.age < 30);

console.log(filtered_persons);

[{name: 'Mary', age: 22, email: 'mary@gmail.com'}, {name: 'Jack', age: 25, email: 'jack@gmail.com'}]

map

Gets elements that have a subset of the original elements’ properties

Array

const persons_name = persons.map(p => p.name);

console.log(persons_name);


['Mary', 'Jack', 'Bob']

join

Concatenates all array elements as a string with a separator

String

const joined_name =

persons.map(p => p.name).join(', ');

console.log(joined_name);

Mary, Jack, Bob

find

Returns the first element satisfying the testing function

object

const first_found =

persons.find(p => p.age < 30);

console.log(first_found);

{name: 'Mary', age: 22, email: 'mary@gmail.com'}

findIndex


number

const first_index =

persons.findIndex(p => p.age > 25);

console.log(first_index);

2

reduce

Reduces array elements to a single value after applying a custom function

It depends on the single value you want to get like number, string, etc.

const initAge = 0;

const sumAges = persons.reduce(

(accumulator, currentPerson) =>

accumulator + currentPerson.age,

initAge,

);

const meanAge = Math.round(sumAges/persons.length);

console.log(meanAge);

28

Blog Posts

Enhancing Performance of Java-Web Applications

Applications built with a Java back-end, a relational database (such as Oracle or MySQL), and a JavaScript-based front-end form a common and...