Operators

Just to refreshing your memory, we will cover the basic things about operators in programming language.

var a = 500;

That equal sign is assignment operator. It can take the literal value of 500 and assign it to variable a.

var a = 100;
var b = 50;

var result1 = a + b;  //150
var result2 = a - b;  //50
var result3 = a * b;  //5000
var result4 = a / b;  //2

The arithmetic operator (+, -, *, /) are very simple. You just have to remember that you can take variable as your operator value. You can even do something like this :

a = a + 50;

//the simpler way
a += 50

In that example, you add whatever value you store before in variable a with literal value of 50.

a++;  //a = a+1
a--;  //a = a-1

The idea of increment or decrement of just adding or subtracting one to a variable is so common. That's why they have their special operator sign like you see in code above.

Whitespace

Javascript does not care abour whitespace. It means you can something like this :

var a=10+5;

or

var a=10+5;var b=10-5;

And it will have the same meaning of this :

var a = 10 + 5;

or

var a = 10 + 5;
var b = 10 - 5;

But as you can see, while Javascript does not care, we as programmers will having a hard time reading the first example. So whitespace is still important. Don't confuse ourselves.

Comments

When you writing more and more code to your program, you should start adding comment to your code, so it can remind you what you trying to do. You add comment in Javascript using double slashes (//).

var a = 10;
var b = 5;

//adding variable a and b
var result = a + b;

If you want adding comment that have more than one line, you can do using opening and ending slash asterisk (/*);

/* this code is
for adding
variable a with b */
var result = a + b;

Like whitespace, comments is not important to our code, but can really handy for programmer to keep track about everything we write. It also helping other people read our code.

results matching ""

    No results matching ""