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:

banner for the css playlist in hebrew life michael courses for programmers

The First Steps in CSS

Learn CSS using our our videos (in Hebrew) on the CSS (he) playlist on youtube. Do it now. Do it for free.

Good Trainers Collaborate with Others

It is always essential to keep an open mind and learn from others. This applies to everyone, including teachers and especially software development trainers. Software

The Beauty of Code

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

Update cookies preferences