0

I'm new to JavaScript. Do you need to declare the var keyword? It appears with the following codes below you don't have to. When do I need to and when do I don't have to use the var keyword? It is it just good practice to use the var keyword?

Without var keyword:

function sample(a,b) {
  result = a + b;
  return(result);
}
output = sample(10,20);
document.write(output);

With var keyword:

   function sample(a,b) {
      var result = a + b;
      return(result);
    }
   var output = sample(10,20);
   document.write(output);
NuWin
  • 276
  • 5
  • 15

0 Answers0