Hey There Guys. Welcome to the third post of this series "Quick and Dirty Introduction to JavaScript". Today we will encounter variables in JavaScript.
Please not that variables are very important part of any programming language , because programming is like playing with data and to play with data we must store it somewhere.
and we store data in variables.try to remember your high schools math's book there ere variables and constants. Yes variables in JavaScript is same like that.
we were using variables in algebra like , a=5,b=10,b=a+b etc. but in JavaSCript we can take variables as full names like
Lets take an example of Basic Math in JavaScript
var first_number = 10; var second_number = 15; var third_number = first_number + second_number; alert(third_number);Creating a Variable in JavaScript is a three way Process.
- Declaring a variable
- Giving it a unique name
- Assign a value to your variable
var dev = 17;Here var is a Keyword (don't be confused we will cover the keywords in JavaScript later ) which tells the JavaScript interpreter that dev is a unique name and 17 is it's value.
Rules you must follow while working with variables in JS
Well there are few rules in JS which you must follow like- JavaScript is Case Sensitive Yes Folks JS is Case Sensitive that means ved,Ved and VED all three are different from each other
- You must begin with a letter,$ or _ That means Ved, My_Num, $My_pay, _NewYork are correct but 1india,%bills,*Jesus are not
- Your variable name must contain number,$,letters and _ You can’t use spaces or any other special characters anywhere in the variable name: fish&chips and fish and chips aren't legal, but fish_n_chipsand plan9are.
Assigning Variables
To assign a variable you must declare a variable then give it a unique name and in last give it a value likevar my_age = 22;Here Var is keyword , my_age is the unique name and 22 is the value of variable my_age
Working with Variables and Data Types in JavaScript
As i told you before that programming is more like playing with data, we play with data and we get new data ummmm like we take three variables a,b and c.we assign values to a and b like a = 5, b = 10 anc c = a+b. see what goes on here that we manipulate the data to get new datavar a = 5; var b = 10; var c = a+b;To manipulate we use operators like in c = a+b, + is the operator. In JavaScript there are different operators for different data types
Basic Mathematics with JavaScript
JavaScript Supports Basic Mathematical operations (see the table below)Lets take an example of Basic Math in JavaScript
var priceOfOne = 15; var totalItem = 5; var totalCost = priceOfOne * totalItem; alert (totalCost);In Next Post i will Post some Simple JavaSCript Programs so that you can do exercise. Thanks for reading, see you in next Post :)
Post a Comment