Default Values for Function Parameters

One feature found in other languages that I missed terribly in JavaScript, was the ability to define a default value for a function parameter. This type of thing is used if you set up a function with several parameters and you want that parameter to have a certain value if no value is passed into the function.

In JavaScript, prior to ES6, there was not a simple way to do this. You could accomplish it with some additional code inside the function body, but it wasn’t very slick.

With ES6, there is now a simple way to define a default value for a function parameter. It looks like this:

var fun = function(x = 10) {
console.log(x);
}

Very simple to do. You simply set the variable equal to a value. If no value is passed in, it takes on the value indicated in the function definition.

Here is a short tutorial that talks about this feature in ES6. It may not be my favorite ES6 feature, but it is contending.

Navigating the Crazy World of JavaScript Libraries and Frameworks

The world of JavaScript libraries and frameworks is exciting, overwhelming, constantly changing, chaotic, and many other adjectives that I can’t think of right now. The popularity of JavaScript has lead to a large number of libraries and frameworks that can help us do our jobs easier.

However, in order to receive that help you have to be able to figure out what is available, find out what it does, and then implement it into your project. It is important to keep abreast of libraries and frameworks, but what is the best way to do that?

I recently found a site that has been invaluable. Javascripting.com contains what is probably the most complete list of JavaScript libraries and frameworks I’ve found. There looks to be over 1200 listed. And it is not just a list; information about each library or framework is provided.

For each one you can access an about page and you can link directly to the repository. Javascripting.com also has a rating system for each item. The rating system is based on several factors, but it can be a good place to start when trying to determine if the library is something you should try. Information from github is also displayed. You can sort the libraries/frameworks by most popular, new or trending.

So…if you want to get up to speed on the latest libraries and frameworks, or check out a particular one you have heard about, navigate on over to javascripting.com and you will appreciate having all this information in one location.

What other resources have you found for JavaScript libraries and frameworks?

Long Live Function Expressions

JavaScript provides two ways to define a function: function declarations and function expressions. I have found myself gravitating more and more toward function expressions. It has become an opinion that I feel I need to express. Not the type of opinion that would sour a nice coding discussion (if there is such a thing), but one I am going to express here.

The function declaration is what I learned first. In fact I really didn’t understand the difference between the two for some time. But now I have moved towards function expressions for the following 3 reasons:

  • Function expressions show the true nature of functions in JavaScript. JavaScript supports first class functions. They can be assigned to a variable as a value and passed around.
  • Certain coding situation may just require a function expression. Therefore function expressions are more flexible in how they are used.
  • Function expressions force me to use more responsible coding because they are not hoisted.

Those are my reasons for generally using function expressions. Which to you prefer and why?

If you need a refresher on the difference, take a moment to watch this video. Happy viewing!