Variables

47 1 0
                                        

Variables is the same thing in math. We assign a value to a letter or a name. Example, let x = 5; This is the same thing here. We use the keyword either let or const followed by the name and then initialize it with a value.

=================

const A = 5;

const B = 6;

let answer = 0;

function times(){

     answer =     A * B; 

     return answer;

}

=================

HTML

document.write(times())

Result:

30


Note:

1. You use the const keyword if the value in that variable will be the same all through out.

2. You use the let keyword if the value will change. In old javascript, they use var instead of let.

3. It is though initializing of variable that the compiler will determine the data type of the variable. In java, it is different, you specify the data type in the definition and then you initialize. In this, the compiler can pin point if the value you encoded has the same data type or not. In javascript, you can always assign a value with a different data type on that let variable.

Example: 

===========

let x = 5;

x = "five";

===========

HTML

document.write(x);

Result:

five


Note: The code above was allowed since javascript is dynamically typed.

Javascript ProgrammingWhere stories live. Discover now