Lambda Expressions in JavaScript PRO

The Harmony (EcmaScript 6) proposal includes the specification for arrow functions, AKA lambda expressions. Given this technology early stage you should check in advance whether the web browser you target supports it.

The following code sample includes the definition for few arrow functions (lambda expressions).

<!DOCTYPE html>
<html>
<head>
    <title>simple lambda expressions in javascript</title>
</head>
<body>
    <script type="text/javascript">
        var funcA = num => 2*num;
        var funcB = num => num%2==0;
        var funcC = (num1,num2) => num1+num2;
        var funcD = (num1,num2,num3) => {
            var temp = num1+num2;
            var result = temp * num3;
            return result;
        }
        document.writeln("<br>"+"funcA(3)="+funcA(3));
        document.writeln("<br>"+"funcB(6)="+funcB(6));
        document.writeln("<br>"+"funcC(3,4)="+funcC(3,4));
        document.writeln("<br>"+"funcD(2,3,4)="+funcD(2,3,4));
    </script>
</body>
</html>

The following video clip overviews this code sample, shows its execution and explains it.

The following code sample shows how we can write shorter code by using lambda expressions when in a need to pass over a function to another function.

<!DOCTYPE html>
<html>
<head>
    <title>useful lambda expression demo in javascript</title>
</head>
<body>
    <script type="text/javascript">
        function Student(frstname,lstname,avg,idnumber)
        {
            this.first = frstname;
            this.last = lstname;
            this.id = idnumber;
            this.average = avg;
            this.toString = function()
            {
                return this.first+" "+this.last+" "+this.id+" "+this.average;
            };
        }
        var students = [
                new Student("mosh","cohen",88,121212),
                new Student("dave","levin",98,324232),
                new Student("leha","lapid",78,434234),
                new Student("roza","efrat",92,423234),
                new Student("yoni","rabin",75,534243)
        ];

        function filter(vec,func)
        {
            var result = new Array();
            var j=0;
            for(var i=0; i<vec.length; i++)
            {
                if(func(vec[i]))
                {
                    result[j] = vec[i];
                    j++;
                }
            }
            return result;
        }

        var vector = filter(students,ob => ob.average>80);

        for(var i=0; i<vector.length; i++)
        {
            document.writeln("<br>"+vector[i].toString());
        }

    </script>
</body>
</html>

The following video clip overviews this code sample, shows its execution and explains it.

You can find more code samples, video clips and training material for learning this topic in my JavaScript Programming free course at http://abelski.lifemichael.com.

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences