Variables are an important concept of computer programming in general.
What are Variables?
If you recall school algebra, there were 'x' and 'y'. Sometimes x had a value of 1, you could add 2 to the value of x and then its value became 3. Thus the value of the letter x varied or changed over time. Though back in school it did not make much sense to why we were using X and Y, why all that much algebra? what was the use? Well in programming you will find them to be very useful.
Simply put, a variable is a container of a value. This container has a name, that we call 'variable name' A variable is represented by a letter like 'x' or any other variable name like combination of letters ex 'customername'. Its value can vary.
Imagine an empty box. If you put carrots in it, it is a box of carrots. At another time it may contain potatoes and then it is a box of potatoes. The contents of the box changes or varies at different instances of time. This box or container has a name like 'smallbox'.
Variables in Programming
In Programming, Variables can hold values or expressions(combination of values and variables joined by operators like +, - , etc). Values can be numbers, text and many other kinds of values.
Example customername = 'Jessica', employeesalary = 60,000
The variable customername has a text value containing customer name, while the variable employeesalary has a numeric value of 60,000.
monthlysalary = employeesalary / 12
The value of the variable monthly salary has a value in the form of an expression. divide employeesalary by 12 (no. of months) and you will get the value for this variable.
Why do we use Variables
We use variables to store data. When we need to use that data in calculations or other purposes, we can refer to the data by referring to its container(variable name) instead of writing the data values themselves. In other words variables represent data.
For example
On top right section of every page of our company website we need to display the total number of Desktop Computers we have sold to date. One way to do it is to change the value of total sales every morning on every page of the website. The other way is to refer to the variable 'Total sales' on every page and update the value of the variable 'total sales' in the system memory, and every webpage thus automatically displays the current value of the Total sales by extracting the information from the main system memory.
If you are still not fully clear about variables, don't worry. You will better understand their use as use them in examples in the lessons ahead. Above description is sufficient foundation knowledge and you can move on.
Declaring a Variable in Javascript
To Create / Declare variables in Javascript, we use the var command.
SYNTAX
var variable_name;
There are two ways to declare a variable.
1. Just declare a variable (assign it a value later)
var countryname; This will create a variable called 'countryname' without assigning value to it.
2.Declare a Variable and also assign value to it.
var countryname = "Australia";
What are Variables?
If you recall school algebra, there were 'x' and 'y'. Sometimes x had a value of 1, you could add 2 to the value of x and then its value became 3. Thus the value of the letter x varied or changed over time. Though back in school it did not make much sense to why we were using X and Y, why all that much algebra? what was the use? Well in programming you will find them to be very useful.
Simply put, a variable is a container of a value. This container has a name, that we call 'variable name' A variable is represented by a letter like 'x' or any other variable name like combination of letters ex 'customername'. Its value can vary.
Imagine an empty box. If you put carrots in it, it is a box of carrots. At another time it may contain potatoes and then it is a box of potatoes. The contents of the box changes or varies at different instances of time. This box or container has a name like 'smallbox'.
Variables in Programming
In Programming, Variables can hold values or expressions(combination of values and variables joined by operators like +, - , etc). Values can be numbers, text and many other kinds of values.
Example customername = 'Jessica', employeesalary = 60,000
The variable customername has a text value containing customer name, while the variable employeesalary has a numeric value of 60,000.
monthlysalary = employeesalary / 12
The value of the variable monthly salary has a value in the form of an expression. divide employeesalary by 12 (no. of months) and you will get the value for this variable.
Why do we use Variables
We use variables to store data. When we need to use that data in calculations or other purposes, we can refer to the data by referring to its container(variable name) instead of writing the data values themselves. In other words variables represent data.
For example
On top right section of every page of our company website we need to display the total number of Desktop Computers we have sold to date. One way to do it is to change the value of total sales every morning on every page of the website. The other way is to refer to the variable 'Total sales' on every page and update the value of the variable 'total sales' in the system memory, and every webpage thus automatically displays the current value of the Total sales by extracting the information from the main system memory.
If you are still not fully clear about variables, don't worry. You will better understand their use as use them in examples in the lessons ahead. Above description is sufficient foundation knowledge and you can move on.
Declaring a Variable in Javascript
To Create / Declare variables in Javascript, we use the var command.
SYNTAX
var variable_name;
There are two ways to declare a variable.
1. Just declare a variable (assign it a value later)
var countryname; This will create a variable called 'countryname' without assigning value to it.
2.Declare a Variable and also assign value to it.
var countryname = "Australia";
Aucun commentaire:
Enregistrer un commentaire