XHTMLboy Posted October 13, 2010 Share Posted October 13, 2010 Hi, i have just started to learn abit about javascript, but i came to a part in a book which i coulden't get my head around. It would be nice if someone could just explane this as easy as possible. I think i get it, it's just in the code what does var answer do. and how does alert(15 / 10); work. var firstNumber = 15; var secondNumber = 10; var answer; answer = 15 / 10; alert(answer); alert(15 / 10); answer = firstNumber / secondNumber; alert(answer); Link to comment Share on other sites More sharing options...
ShadowMage Posted October 13, 2010 Share Posted October 13, 2010 Hi, i have just started to learn abit about javascript, but i came to a part in a book which i coulden't get my head around. It would be nice if someone could just explane this as easy as possible. I think i get it, it's just in the code what does var answer do. and how does alert(15 / 10); work. var answer; just declares a variable named answer. It does not initialize (assign a value) to it yet.alert(15 / 10); just alerts the value of 15 divided by 10 (which is 1.5). Anything inside the parenthesis is evaluated first then a message box is appears showing the return value of whatever expressions/statements were inside the parenthesis. Link to comment Share on other sites More sharing options...
XHTMLboy Posted October 13, 2010 Author Share Posted October 13, 2010 var answer; just declares a variable named answer. It does not initialize (assign a value) to it yet.alert(15 / 10); just alerts the value of 15 divided by 10 (which is 1.5). Anything inside the parenthesis is evaluated first then a message box is appears showing the return value of whatever expressions/statements were inside the parenthesis.Oh ok, so alert(10 /15); does not need a varable. And var answer; holds what? Link to comment Share on other sites More sharing options...
thescientist Posted October 13, 2010 Share Posted October 13, 2010 Oh ok, so alert(10 /15); does not need a varable. And var answer; holds what?well, first it holds nothing, as ShadowMage explained. Then it is assigned the value of 15/10. Then it is assigned the value of firstNumber/secondNumber, which is also 15/10, which yields the same value as 15/10. Link to comment Share on other sites More sharing options...
aspnetguy Posted October 13, 2010 Share Posted October 13, 2010 var firstNumber = 15; //this variable is declared and set to a value of 15var secondNumber = 10; //this variable is declared and set to a value of 10var answer; //this variable is declared but no value is assigned to it. It currently is null or undefinedanswer = 15 / 10; //the expression is evaluated and a value of 1.5 is assigned to the variablealert(answer); //alerts the value of the variable answer which is currently 1.5alert(15 / 10); //evaluates the expression and alerts the result - 1.5answer = firstNumber / secondNumber; // evaluates the expression and the values of the variables used (15/10) and assigns the result to answeralert(answer); //alerts the current value of answer which is still 1.5 Link to comment Share on other sites More sharing options...
XHTMLboy Posted October 16, 2010 Author Share Posted October 16, 2010 var firstNumber = 15; //this variable is declared and set to a value of 15var secondNumber = 10; //this variable is declared and set to a value of 10var answer; //this variable is declared but no value is assigned to it. It currently is null or undefinedanswer = 15 / 10; //the expression is evaluated and a value of 1.5 is assigned to the variablealert(answer); //alerts the value of the variable answer which is currently 1.5alert(15 / 10); //evaluates the expression and alerts the result - 1.5answer = firstNumber / secondNumber; // evaluates the expression and the values of the variables used (15/10) and assigns the result to answeralert(answer); //alerts the current value of answer which is still 1.5 thankyou for your help Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.