Teddy
Teddy

Reputation: 645

What is the role of variables (var) in Javascript

I am currently learning JavaScript and I am wondering what is the role of the variables (var).

In the example bellow, on the last two lines we first define a variable "monCompte" in which we call "john.demandeCaissier(1234)". Then we use console.log(monCompte) to print the result on the screen. What I don't understand is why do we first need to define the variable "monCompte" to call "john.demandeCaissier(1234)". Why can't we just do something such as:

console.log(john.demandeCaissier(1234));

Example

   function Personne(prenom,nom,age) {
   this.prenom = prenom;
   this.nom = nom;
   this.age = age;
   var compteEnBanque = 7500;

   this.demandeCaissier = function(mdp) {
     if (mdp == 1234) {
         return compteEnBanque;
     }
     else {
        return "Mot de passe faux.";
     }
   };
}

var john = new Personne('John','Smith',30);
var monCompte = john.demandeCaissier(1234);
console.log(monCompte);

Thank you for you answers.

Upvotes: 0

Views: 251

Answers (4)

M3579
M3579

Reputation: 910

You could definitely do that, but in more complex programs you will need to store variables for several reasons:

Shortening Long Expressions

Imagine if you saw this code somewhere:

console.log((parseInt(parseFloat(lev_c + end_lev_c)) - parseInt(parseFloat(lev_c + start_lev_c)) + 1));

BTW I got that from here

Wouldn't it be so much simpler just to split that expression up into different variables?

Storing Data

Let's say that you take some input from the user. How would you refer to it later? You cannot use a literal value because you don't know what the user entered, so do you just call the input function again? No, because then it would take the input a second time. What you do is you store the input from the user in a variable and refer to it later on in the code. That way, you can retrieve the value at any time in the program.

If you are a beginner, you might not see any use for variables, but when you start writing larger programs you will start to use variables literally in almost every line of code.

Upvotes: 1

spender
spender

Reputation: 120480

Yes, you can inline your function call and avoid the need for a variable. However, if an error occurs on that line, it becomes harder to debug:

var monCompte = john.demandeCaissier(1234);
console.log(monCompte);

vs

console.log(john.demandeCaissier(1234));

in the second example, there are several different modes of failure that would not be apparent in a debugging session. When split over two lines, some of those failures become easier to track down.

Second, if you wanted to reuse the value returned by john.demandeCaissier(1234) (the author might have shown this), then a variable becomes very useful indeed.

In my opinion, it's a worthy pursuit to perform only a single operation per line. Fluent-style advocates might disagree here, but it really does make debugging considerably easier.

Upvotes: 3

Felipe Skinner
Felipe Skinner

Reputation: 16626

Variables are structures that store some value (or values). They're only that and you could probably do all your code (or the majority of it) without them.

They help you organize and add some readability to your code. Example:

alert(sumNumbers(askNumber()+askNumber()));

takes a lot more effort to read/understand then this:

var firstNumber = askNumber();
var secondNumber = askNumber();
var total = sumNumbers(firstNumber + secondNumber);
alert(total);

Sure it's longer but it's more readable. Of course you don't have to use var for everything, in this case I could just hide the total.

Another common use for variables is "caching" a value.

If you had a function that sums like 1 million values, if you keep calling it for everything, your code would always have to do all that hard work.

On the other hand, if you store it on a variable the first time you call it, every other time you need that value again, you could just use the variable, since its a "copy" of that calculation and the result is already there.

Upvotes: 1

Major Productions
Major Productions

Reputation: 6042

Variables exist to store data. They're useful because instead of invoking an operation over and over again, which is criminally inefficient, they allow you to invoke an operation once, and then use that result where necessary.

And that's for all languages, not just JavaScript.

Upvotes: 1

Related Questions