var functionName = function() {} vs function functionName() {}

I’ve recently started maintaining someone else’s JavaScript code. I’m fixing bugs, adding features and also trying to tidy up the code and make it more consistent. The previous developer used two ways of declaring functions and I can’t work out if there is a reason behind it or not. The two ways are: var functionOne … Read more

How to check whether a string contains a substring in JavaScript?

This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. Usually I would expect a String.contains() method, but there doesn’t seem to be one. What is a reasonable way to check for this? 3 ECMAScript 6 introduced String.prototype.includes: const string = “foo”; const … Read more

How do JavaScript closures work?

This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves? I have … Read more

What does “use strict” do in JavaScript, and what is the reasoning behind it?

Recently, I ran some of my JavaScript code through Crockford’s JSLint, and it gave the following error: Problem at line 1 character 1: Missing “use strict” statement. Doing some searching, I realized that some people add “use strict”; into their JavaScript code. Once I added the statement, the error stopped appearing. Unfortunately, Google did not … Read more