Home » Guides Beginner Article

Use functional programming techniques to write elegant JavaScript

3.0/5.0 (2 votes total)
Rate:

Shantanu Bhattacharya
June 14, 2006


Shantanu Bhattacharya
Shantanu Bhattacharya has extensively designed and created architecture for application software for retail, system integration, and healthcare; networking software that's SNMP-based, and TCP/IP stack; security software; a file system for India's first supercomputer; and Real Time Software for the Indian Missile Program. Currently he is working in the healthcare industry for Siemens in Bangalore, India as a chief architect.

Shantanu Bhattacharya has written 1 articles for JavaScriptSearch.
View all articles by Shantanu Bhattacharya...

Functional, or declarative, programming is a very powerful programming method and is gaining popularity in the software industry. This article introduces some of the relevant functional programming concepts, and provides examples to use those concepts effectively. The author explains how to write elegant code with JavaScript, which can import constructs and features from functional programming.

Introduction

Functional programming languages have been in academia for quite some time, but historically they do not have extensive tools and libraries available. With the advent of Haskell in the .NET platform, functional programming is becoming more popular. Some traditional programming languages, such as C++ and JavaScript, import constructs and features from functional programming. In many cases, repetitive code in JavaScript leads to clumsy coding. You can avoid all that if you use functional programming. Also, you can write more elegantl callbacks with a functional programming style.

Functional programming

Functional programming describes only the operations to be performed on the inputs to the programs, without use of temporary variables to store intermediate results. The emphasis is to capture "what and why" rather than the "how." It emphasizes the definition of functions rather than the implementation of state machines, in contrast to procedural programming, which emphasizes execution of sequential commands.

Large scale knowledge management applications benefit greatly from using a functional programming style as it simplifies development.

Because functional programming encompasses a very different way of composing programs, programmers who are used to the imperative paradigm can find it difficult to learn. In this article you'll see examples of how to write good, elegant code in JavaScript in a functional style. I'll discuss:

  • Functional programming concepts, including anonymous functions, different ways to call functions, and how to pass functions as arguments to other functions.

  • Use of functional concepts, with examples of: extending array sort; elegant code for dynamic HTML generation; and application of a sequence of functions.

 

 

 

 

 

 

 

 

Read on... 


Add commentAdd comment (Comments: 0)  

Advertisement

Partners

Related Resources

Other Resources

arrow