Tidbit Tuesday – Getting started with JavaScript

Today we are going to be taking a tiny look at JavaScript, especially how it differs from other C-based languages (C++, C#, PHP).  *** Unity uses it’s own version of JavaScript and the data below is not 100% relevant.

  • JavaScript is case-sensitive & uses the Unicode character set
  • JavaScript is a Weakly Typed – Object based language
  • There are 3 main variable types: number, string, boolean – along with null & undefined
  • Instructions are called Statements and SHOULD be separated with a semicolon
  • Procedures are called Functions, if the function is inside of an object it is called a Method

End of the Statement

If you noticed above I say that statements SHOULD be separated with a semicolon, this is because JavaScript will assume (in most instances) that if you start a new line that you wanted there to be a semicolon and it interprets it as such. A lot of old-school JavaScript developers are in the bad-habit of not putting in semicolons, this can cause unforeseen errors that can be difficult to track down; it also makes your code harder to read. So for ease of reading, and to prevent unwanted errors ALWAYS:

  • Have only 1 statement per line
  • Keep each statement on a single line
  • End your statements with a semicolon  ;

Weakly Typed Confusion

Because JavaScript is weakly typed you can do things like this:

var userID = “cat”;
userID = false;
userID = 123;

Variables can be easily changed from one type to another and JavaScript does it’s best to try and figure out what you are trying to do, and because a variable can change types we get an extra comparison operator.  == checks to see if the values are the same, where === checks to make sure the value and the type are both the same.

userID == “123” // true
userID === “123” // false
null == undefined // true
null === undefined // false

I throw in the null part because JavaScript will also try and reinterpret null & undefined depending on the syntax…. Either 0 (zero), an empty string, or false.  However it is always good practice to check for null, undefined or type if you are not 100% sure what type of data may be getting passed in; don’t just assume that it will be interpreted correctly. To help out with this we have parseInt(), parseFloat(), instanceof, typeof, and many more. If you are coming over from a Strongly-Typed language the biggest hurdle you will have is that your IDE will not throw errors when you attempt to pass in the “wrong” type of data to a function.

Try It Out

Most browsers have a built in developer console that you can get to just by pressing F12 then clicking on the correct tab.  After you’ve done that you can type in any JavaScript code you like and it will run it for you.  Copy this:  alert(“It Works!”); – hit F12 and see if you can get it to run.  To close the developer console just hit F12 again.

Now look at the following code-snippet.  See if you can figure out what it is going to do – then run it in your developer console (F12) and see if you were right:

So did it do what you thought it would do?  The weakly typed nature of JavaScript does things with variables you may not expect. Go ahead and try out your own code now – use SHIFT-RETURN to go to a new line – or write your code in notepad and paste it into the console.  If you come up with anything super cool post it in the comments.

As always questions and constructive criticism are both welcome and encouraged!

Posted in Beyond Mindless, Code Snippets, Programming and tagged , , , .