Jump to content
Sign in to follow this  
XHTMLboy

Variable HELP!

Recommended Posts

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);

Edited by XHTMLboy

Share this post


Link to post
Share on other sites
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. Edited by ShadowMage

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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. Edited by thescientist

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...