Variables in JavaScript
Declaring variables in Ruby are very straight forward. Since Ruby provides syntax sugar for those who are using its program, it makes a job pretty easy. Ruby has class, instance, and a local variable, that has a clear purpose and can be used as a straight forward reason. On the other hand, JavaScript also uses variables just like Ruby program, that has a similar purpose, but different in many ways. Let take a look at some examples.
Let’s start with Ruby first, now we are using with a local variable that ‘sayHello’ will have the purpose of storing “hello!”. Now whenever we need to use sayHello, or call the local variable just like example image below, it will print out the given value.
As the standard rule for declaring variables on Ruby, it’s always good practice to set a value to a variable when it’s the first declare to avoid any unnecessary bugs or errors. So what would happen if the variable ‘sayHello’ was declared at the statement but without any given value? Yes, of course, it will trigger error!
The Ruby program reads from the top line to the bottom of the code. Although we have declared the variable ‘sayHello’ that has nothing in it, and by the time we call, Ruby simply caught an error, saying it doesn’t know what to do even we have later set the value to the variable ‘sayHello’ later on.
But! let’s now take a look at JavaScript. From the examples above its crystal clear that when declaring a variable setting value must require, right? JavaScript refuse that! What does that mean? Let’ take a look!
Ok, wait…hold on..what??? It’s obvious that the variable ‘sayHello’ will print out “hello!” since we declare the variable by giving a statement “hello!” but what happened to the variable ‘sayBye’? It turns out unlikely with Ruby, JavaScript can declare the variable without any value, meaning you don’t have to store anything to start with it. Instead of poping an error, JavaScript read the variable ‘sayBye’ that has no value, simply as undefined. It’s like we have car storage without a car inside. If the car is not inside the storage, it doesn’t mean that car storage doesn’t exist. It still has purpose to be store later on when a car comes in, but simply it’s empty for now. JavaScript does similar to the car storage example. It recognizes the variable just as undefined and waiting the be store later on.
Strange…..I know, and for reasons like this, many people who have studied Ruby and trying to learn JavaScript may be caught a lot of confusion and trouble with digesting the whole different concepts of ideas. It more surprising with ‘var’ that can scope inside or outside the function(in ruby calls method) will even add more mystery to those new to JavaScript. Luckily JavaScript introduces the new way of declaring a variable with ‘let’ and ‘const’. Best practice for anyone who is new to JavaScript like me! ( and who just jump into the world of mystery from Ruby to JavaScript), it’s the best way of avoiding any trouble by stick with ‘let’ and ‘const’ when it comes to declaring a variable!