View Categories

Five JavaScript Tips for PHP Developers

  • Jan 30, 2015
  • 0
  • by A2 Marketing Team

PHP and JavaScript both have a similar, C-like syntax. This can be both a help and a hindrance to PHP developers who are beginning to write JavaScript. The familiarity can both help and lead them astray. I thought it might be useful here to compile a list of some stumbling blocks to avoid and practices to adopt in order to ease the transition into writing JavaScript.

Type out your own semicolons. One thing new JavaScript developers sometimes think is cool is that JavaScript allows you to skip writing your own semicolons to end your statements. The JavaScript interpreter will assume where you wanted them based on your newlines and put them in for you. But it doesn’t do a perfect job, and sometimes you will have ambiguity that will lead to problems in your code. Not only that, but your code will be harder to read without explicit semicolons. Don’t fall into this trap.

Use dot notation. JavaScript allows you to use square brackets such as foo[‘bar’] just like you would in PHP. But JavaScript also allows for dot notation such as foo.bar. The current convention is to use dot notation most of the time, and only use square brackets when you need to, such as accessing by value (e.g. var baz = ‘bar’; foo[baz].) Dot notation is preferable in JavaScript, since not only is it 3 fewer characters, but it is also easier to read. Especially when you are accessing Object properties which are functions like foo.bar().

Don’t use globals. You shouldn’t be using globals in PHP either. But in case you think ti’s OK in JavaScript, it’s not. It’s even worse in JavaScript, because your global variable (a variable declared explicitly or implicitly at the top level of your JavaScript code) is available everywhere, and might break someone else’s code that you’re trying to include in your page. Which leads us to the next tip.

Encapsulate your code. Don’t just put a script tag on your page and start writing code in it. Put it inside of a new function and then call that function within a document ready function. Isolate your code from any other code that may be running on your page. This will help prevent your code from breaking other scripts you want to include on the page. Even from multiple scripts of your own conflicting with each other.

Use triple equals (‘===’). In PHP we use the simple foo == bar in conditionals and it mostly works. In JavaScript double equals is just completely broken. It tries to help you, but in doing so will give you totally unintended results. Instead, you should use triple equal (‘===’) to check for actual complete equivalence in two values. This also requires you to write your code so that you really are checking for truly intended values. Which, if you weren’t doing this already, will improve the quality of your code.

There’s a lot of other practices and tips that may help you out. For a good overview I recommend checking out JavaScript: the Good Parts which will cover most of the above points in more detail, as well as many others. If you follow the advice given by Douglas Crockford you will find writing JavaScript to be a much less stressful exercise.

The A2 Posting