In this chapter we would cover Data Types and Variables.
We would cover the following:
Boolean: This is data type for True or False values
There is also two trivial data types: null and undefined. We would use these more in later sections.
You probably have heard of variables. All programming languages use variables. A variable is like a container to hold data. Hence you can assign values to variables. However before you can use a variable, you must declare it.
To declare a variable, you use the var keyword. The code below declare three variables.
After you declare a variable, then you can assign a value to the variable. This is called initialization. A variable can also be initialized the same time it it declared. For example, the code below declares the variables and assigns them at the same time.
The concept of variable scope actually applies across programming languages.
So, what is a variable scope?
Local Variable: A local variables is a variable that is only visible within the function where it is defined. Function parameters for example. Hence, local variables are not visible outside the block
Inside the body of a function, local variables take precedence over global variables. These means that if there is a global variable with the same name, then the local variable is used. Let’s take an example:
In the above code, when you run this page, then “local variable” is displayed. This is because, the write statement was done inside the function.
b. Variable names must not start with a number eg 0-9. Variable names must begin with a letter or an underscore. For instance score32, _firstname, address.