DEV Community

Cat4eve
Cat4eve

Posted on

JS#1, JavaScript variable differences

Made by Karapet Khachatryan, AUA CS freshman student.

JavaScript or JS is a popular programming language all over the world. It is used by many comapnies and individuals. The language is pretty easy to understand for a newbies, since there is not such complicated stuff like typization, memory management, mandatory semicolon, etc. This course will be about the basics of JS to show that everyone can learn it.

First of all it's important to be aware of the syntax or writing style for JS. Syntax predefines how the code must be written for a computer to understand. There are several general strict rules and personalized writing styles that have been developed during years among developers or programmers who had a huge experience working with JS. It will be better to show and explain in examples, explaining how code works and performs.

The basics of programming are variables. In math we have symbols, which we name numbers, for example, 1, -3, 4.3, etc. Same numbers exists for JS, however, in real life we memorize them unconsciously, however, computers can't do that, which means that we must tell them what to memorize. The main way we can tell computers that it must memorize something is to create a variable. Variable are pretty much similar to our brain cells or neurons, today's computers have enough memory to compete with people's brain capacity. There is an example of JS command.

var x;
Enter fullscreen mode Exit fullscreen mode

In a few words, we asked computer to create a variable inside its memory with var word at the begining of the text line. Next, we asked computer to name that variable as x, because a programmer doesn't know what exactly is going on inside the computer, so the only way not to lose the track of that variable among the terrabites of other variables is to have a name tagged with it. Finally, the semicolon or ; symbol is not mandatory and the same code would work equally with or without it. The semicolon has a unique story in the world of programming, but for this course we would not explore it.

As i've mentioned, in JS we have general numbers and we would write them exactly in the same way. The variable we have created is empty. Likewise our brain cells, it can be stored with some kind of information. With this information, let's create a variable x and give some number to store inside it.

var x = 2;
Enter fullscreen mode Exit fullscreen mode

What have changed from previous example?

We have introduced two new symbols - = and 2 (once again, semicolon is not necessary to have). The number two has been used as an information to be stored inside the variable x, while equal sign tells us that we want to estabilish the equality between x and 2, which in the language of programming means that we want to assagine 2 to the variable x. It is important to mention that we write floating and negative numbers exactly likewise in real life, e.g. -3, 4.6, etc.

What else we can store inside those variables?

In JS we have variable types. One of them we've already learnt - numbers (3, -5, 3.4). Next, we have texts, which are named as string types. To write them we use single or multiple quotation marks ("" or ''). As an example, "Hello", 'Hola', "Bon jure". Finally we will observe boolean types (named after George Bool) during this course. In boolean type there are only two expressions - true and false, which stands for truth and not truth.

There have been a lot of information so, let's conclude with examples

var a = 5;
var b = "Shalom";
var c = true;
Enter fullscreen mode Exit fullscreen mode

Cool, we have somehow learnt about variables, right? Let's learn about the history!

Historically, in previous versions of JS there existed only one way to create a variable - var, next, JS acquired two additional ways to do the same - let, const. However, there is no need two have three equivalent expressions, hence, they have some differences.

We use const to define a variable which can not be changed (read only). Actually its ability is not always valid, however, this would be covered in the next topics.

let is pretty much equivalent to var. The difference between them is not obvious and let share that difference with const too. The difference's name is variable hoisting. Since, var is old it has a tiny bug, which we will observe together.

x = 5;
var x;
Enter fullscreen mode Exit fullscreen mode

Hint - this code is logically wrong and must not work in general, since 5 has been assigned to variable x, and only then x variable has been defined with var keyword.

Since we work with JS (one of the funniest programming languages), this writing would work fine. After those two lines, the variable x would have a value 5.

x = 5;

let x; // or // const x;
Enter fullscreen mode Exit fullscreen mode

In those cases we would have an error, which is right logically, there is no way we can work with variable without declaring it.

Top comments (0)