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.
console.log(john.demandeCaissier(1234))console.log(new Personne('John','Smith',30).demandeCaissier(1234));, but each time you put more and more things on one line it becomes increasingly difficult for a human to read.