Javascript: Oh, Take Me Back to the Start

The title of this post comes from a lyric in “The Scientist” by the band Coldplay. It seemed to best sum up my quest to refresh my perspective on JavaScript by completing a course on Pluralsight called Basics of Programming with JavaScript by Kyle Simpson. I wanted to find out after many years of programming in the language if I had the basics right. The course was very good in both introducing JavaScript but also the concepts of programming itself.

Here is a little breakdown of the concepts that stayed with me following the course (besides calling it JavaScript instead of Javascript after noticing the title and a little Web search. Although, like a number of things to do with the actual language, it’s really not a big concern which one you use).

Getting the word out
console.log could be a better bet to use than alert() in your JavaScript program to display a message to yourself, which is useful for debugging. The latter is a feature of the browser, not the language. So if you are programming an app in JavaScript, alert() may not work while console.log should. Good practice to put in place for either type of development. I had always been a user of alert() but should consider console.log and getting used to viewing the message in the console of the browser.

Don’t leave me hanging…
It is indeed best practice to use a semi-colon to end your statements, even though JavaScript can forgive it’s absence. It makes the code easier to read for the developer. I personally prefer this when possible and try to follow it when I can.

However, don’t use that semi-colon here
In a for loop, you just cannot use a semi-colon after the update clause (i++), so don’t worry about it again (I told myself), even if it does look odd.

Singles or Doubles?
JavaScript also doesn’t concern itself with whether you use single or double-quotes for things like defining a string. But again it’s easier for developers if you pick one in your code and stick to it. From my experience, consistency in use of quote style can also mean less errors. It was noted that single quotes may be easier to type. I had been more of a double-quote person myself, a leftover from some database programming days, but considering singles now to keep things lighter and faster.

Block style
It again doesn’t matter to JavaScript if you place your opening curly brace of a block you are attaching to a statement flush with the statement or on the next line, but from my own experience, it’s so much easier to read if you keep it consistent. Again, the theme of thinking of the developer. I bounce back and forth between which one I prefer here, but will strive to at least use it consistently in a program.

Know your parameters and avoid an argument
When you define a function, the variable passing in a value is called a parameter. When you call the function and pass in a value, it is called an argument. I tended to call them both parameters so good to know.

Variables don’t know who they are, but their values do (kind of existential don’t you think?)
JavaScript variables don’t have a type, they are like an empty shell. But once assigned a value, they then take on that value’s type. You complete me indeed.

Speaking of variables, identify yourselves
It’s another best practice to formally declare your variables i.e. var myvar;, even if JavaScript again doesn’t worry much about it. I try to follow this when I can as well as I find it’s easier to find them that way and I like to know where they started out on their journey in the program.

So many ways to be wrong
“falsy” (false) in the JavaScript world is when anything turns out to be drum roll…0, -0, NaN, ” (or “” hey), false, null or undefined. Everything else rings “truthy” (true). This is a good one to keep in the old back pocket.

Order in the chaos
You can have variables and functions in various orders throughout your program, and sometimes it can affect how things carry out and sometimes not. But again consistency is key and it’s best to follow a practice like the author suggests of functions at the top followed by variables. I tend to keep them grouped but switch the order, and will consider his approach too.

Comment for others and yourself
Finally, if something will need an explanation of why or how, then a comment may be needed for your future self and others. This will help me in those times of questioning if a comment is needed here.

This course reminded me that JavaScript is not the strictest language in the way it goes about things, but it’s important to keep the code nice and clean for the developers working in it. The author said it best in that the program is for the computer but how it is written is really for the developer. It was good to see that I was on the right track and/or where to tighten the screws when it came to my JavaScript basics. I’ll try to keep these points in mind in my future coding.

Pluralsight has an advanced JavaScript programming course as well as some other JavaScript topics by the same author that look quite interesting. Life has gotten busier in all the ways it does and maybe I’ll get to take a look at them and more sometime, but for now I’m happy with my little stress test on how I’m going about my JavaScript programming. It’s wonderful to know there are so many great resources out there and folks ready to teach when you want to refresh skills or learn something completely new, and that if you give it a little time and effort, it’s amazing what you can learn. Keep well and keep learning in all aspects of your life dear coders!

Leave a Reply

Your email address will not be published. Required fields are marked *