WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You

WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Accespted Javascript - Variables Mon Mar 12, 2012 9:02 am

Mr.Joker

Mr.Joker
Mr. WebArtz
Mr. WebArtz
JavaScript - Variables
What are variables - With Examples


Hello everyone. Today I will show you what is variable and how to use it in javascript. Before I go further, you need to know that variable is some word that can storage a value in it. For example you learned in schoold that variable x can be any number. x is variable because it can store a value which is number in Math, but also it can store a characters like words. Lets frist see most simple javascript code:

Code:
<script type = "text/javascript">
document.write("Hello there.");
</script>
When we look at this code we see something really important which is command document.write . Whit that command we say to our web browser to write something on the screen. "Hello there." is sentence that will be printed on the screen. Why i show you this? Maybe you didnt know how this work so I had to. Lets move on. Now i will show you how to use variable in javascript:

Code:
<script type = "text/javascript">
var x = "Hello there.";
document.write(x);
</script>
Ok, now we look the code again and wonder what is different. Now we see something new , which is var x. So what is that? Let me explain it better:
var - is abbreviations of variable.
x - is name of variable. It could be anything else. Any other letter or word. After we name our variable we write that its equal to some value. Its important you understand something. If you want the value be string (words, characters) you will use "" <--- that . But if you want the value of variable be some number you dont need quotation marks. Just a number. Why? Because its an integer, and an integer is writen like that in javascript. Now lets see use of some integer value of variable:

Code:
<script type = "text/javascript">
var x = 5;
document.write(x);
</script>
When we run this code we well see a number 5 on the screen which is good. Now lets learn to addition with variables. Its really fun, trust me.

Code:
<script type = "text/javascript">
var x = 5;
var y = 7;
var z = x + y;
document.write(z);
</script>
Ok , now you wonder, what you did here? Well, I did really simple thing for you. I made two variables, one is x with value of 5, and other is y with value of 7. Then i written a third variable with value of addition this two. Its simple. I said use variable x and variable y to preform addition and store that value in variable z. Its like math but more simpler. How to do subtraction? Same way just set minus.
Code:
<script type = "text/javascript">
var x = 5;
var y = 7;
var z = x - y;
document.write(z);
</script>
Our web browser should print -2 as final result which is true. How to do now multiplication? Again, same way just other sign.
Code:
<script type = "text/javascript">
var x = 5;
var y = 7;
var z = x * y;
document.write(z);
</script>
The result should be 35 if I am good with math still Very Happy . And last one is divison. And once more, same way different sign.
Code:
<script type = "text/javascript">
var x = 5;
var y = 7;
var z = x / y;
document.write(z);
</script>
Final result should be 0.7142857142857143 as i think (of course web browser told me so) Very Happy . So you think now, hey cool i know all about variables. Well, its wrong. I have more thing to tell you.Now we must learn that you can also preform addition with variables that have value string (text). Sound strange now but lets see how it look:

Code:
<script type = "text/javascript">
var name = "Aleksandar ";
var last_name = "Arsic";
var full_name = name + last_name;
document.write(full_name);
</script>
I say to web browser now, hey, there is a variable with name (name) and that variable have value of string which is "Aleksandar ", with bit of space after last letter. Then i made second variable , again string value (Arsic). Whats next? A new variable with value of string (first variable + second variable). So how it works? It work so simple. Image you write a word on one peace of paper, then write another word on second one. And you say, Ok, i have a table, let use it as my third variable and thats the place where i combine first two variables. So you just do addition with words and get a name. Thats how it works on some stupid example that i created for you. Now, the last thing you have to udnerstand and most important. Look examples below:
Code:
<script type = "text/javascript">
var x = "5";
var y = 6;
var z = x+y;
document.write(z);
</script>
Ok, you look at code at sudenly you say, well I know this, a final result will be 11 and of course you was wrong. The result of this is 56. Huh? But why? You see that first variable have a string value, we used quotation marks and you have to see it. So if its string that mean a number is not a number anymore, its just a value, like a text. Second variable is a integer (number). And whenever you preform addition betwen string and integer the result must be always string. Another example:
Code:
<script type = "text/javascript">
var x = "5";
var y = "6"
var z = x+y;
document.write(z);
</script>
Both variables are string, and the result must be always string then. So the result is 56. Remember the paper example, just add one paper near other. Last example:
Code:
<script type = "text/javascript">
var x = 5;
var y = 6;
var z = x+y;
document.write(z);
</script>
Now we have two integers (numbers) so the value must be always integer which is 11 in this case. here are this rules:

1. String + String = String
2. String + Integer = String
4. Integer + Integer = Integer

Hope you like my tutorial.

Notice : This tutorial is copyrighted by WebArtz Forum. You may not publish it on anywhere without written permission from the administrators.

2Accespted Re: Javascript - Variables Mon Mar 12, 2012 10:58 pm

ankillien

ankillien
Administrator
Administrator
This is really nice tutorial. Very well explained.
Though we already have a tutorial about JS variable, I'll accept this one Smile

Thanks Mr.Joker Very Happy

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum