Functional JavaScript

Functional programming in JavaScript has been a topic of considerable discussion for some time now. Several books have been published about functional programming in JavaScript and there are developers that use this approach almost exclusively.

Whether or not JavaScript is a functional programming language is beside the point now because so many people are using it for such.

I don’t know enough about functional programming to teach the ins and outs of that particular approach. However, what I do know is that functional programming has some very valuable concepts that you can draw on to improve your code. I’m advocating a pragmatic approach to functional programming in JavaScript. In other words, use elements of functional programming that you find valuable.

So this begs the question, how to get started? Well, as mentioned there are several books published about the subject, and I will be reviewing some of those in the future. But I think a great first step is to really understand some of the methods of the Array object. These methods were designed around functional programming concepts. For example, map, forEach, every and reduce are a few that are valuable to understand. And it just so happens we have a tutorial on some of those methods.

What are your thoughts on functional programming in JavaScript?

You can find the tutorial here:

Exception Handling

Exception handling is an important aspect of JavaScript coding. I know that when I first learned exception handling, it opened for me new horizons on how I could approach problems. If an exception occurred because of circumstances that were not ideal, I could react to that exception and still keep my programming running.

The main mechanism for exception handling in JavaScript is the try catch statement. It is great for exception handling, but it is also possible to use try catch in situations where there is not an exception.

I just completed two tutorials on exception handling. The first shows how to use try catch statements in your code. The second talks about when you should and should not use try catch.

Take a look at both videos and if you have any additional rules to add for using try catch statements post them here.

Obfuscation in JavaScript

I love the word obfuscation. It seems to reflect its own definition. To obfuscate something is to make it difficult to understand.

So why would we want obfuscation in JavaScript. Well, let me give you an example. Recently I was working on a project where we had to connect to an online system. The online system did not have the security fully implemented yet, and as a result we had to include some of the details about the system in the JavaScript code. Well, we didn’t want it to be super simple for someone to discover that information. We wanted to hide it a bit. So the solution…we obfuscated that portion of the JavaScript code.

I learned about a couple of sites that help do this. They obfuscate your JavaScript code by moving things around and converting information to Hex. I used both of these locations. I would pass the code through the first location and then pass the results through the second. Technically, someone could figure it out, but it is quite difficult. Drop some code in, obfuscate it, and then see if you can make heads or tails of it.

https://javascriptobfuscator.com/Javascript-Obfuscator.aspx

https://www.daftlogic.com/projects-online-javascript-obfuscator.htm

Objects, the Center of the JavaScript Universe

When someone says that JavaScript is not an Object Oriented Language, what they really mean is that JavaScript doesn’t use the approach to object orientation that they are used to. It doesn’t use classes. It is not necessary to create a class in order to instantiate an object. To me that is a big advantage with JavaScript.

To say that JavaScript doesn’t use objects is very far from the truth. I will put it this way: it is almost impossible to do anything of value using JavaScript without using objects. Everything but primitive values are objects. Arrays are object. Functions are objects. A regular expression definition is an object. A date value is an object. And it goes on and on.

So objects are central to everything in JavaScript. Because of this point I have started compiling (in the form of a playlist) a bunch of tutorials on objects in JavaScript. There are numerous topics that could be covered with objects, so this compilation is not complete, but that is what I would like to work towards.

If you haven’t taken the time to really sink you teeth into objects and understanding the object oriented nature of JavaScript, you need to! It needs to happen sooner rather than later. You can start with this playlist.

Then get a book on objects in JavaScript and dive deeper. I have reviewed a couple as a part of this blog. Here are two good ones.