How to JavaScript Part 1

How to Comment:

Initializing Variable with the Assignment Operator

It is common to initialize a variable to an initial value in the same line as it is declared.
var myVar =0;

or

var a = 9;

meaning a = 9

Understanding Uninitialized Variables


When Javascript declares variables, they have an initial value of undefined. Undefined mathamatical operations result in "
NaN" or "Not a number" Here is an example of taking undefined variables to defined.

Before:
Initialize these three variables
var a;
var b;
var c;


Do not change code below this line

a = a + 1;
b = b + 5;
c = c + " String!";

After:

Initialize these three variables
var a = 5;
var b = 10;
var c = "I am a"

Do not change code below this line

a = a + 1;
b = b + 5;
c = c + " String!";

camelCaseing


JavaScript is case sensitive and (Oh crap I probably made a million empty pages on the wiki doing this...) and is really important.

Adding/ Subtracting/Multipling and Dividing Numbers

Number is a data type in
JavaScript which represents numeric data. Use + to add two numbers together (duh?)

Add:
myVar = 5 + 10; assigned 15

Subtract:
var difference = 45 - 33;

Multiply:
myVar = 13 * 13;
assigned 169

Subtract:
myVar = 16 / 2; assigned 8

Increment a Number (add one to a number )

Finding a Remainder in JavaScript

The remainder operator % gives the remainder of the division of two numbers.

Example

5 % 2 = 1 because
Math.floor(5 / 2) = 2 (Quotient)
2 * 2 = 4
5 - 4 = 1 (Remainder)
Usage
In mathematics, a number can be checked even or odd by checking the remainder of the division of the number by 2.

17 % 2 = 1 (17 is Odd)
48 % 2 = 0 (48 is Even)
Note
The remainder operator is sometimes incorrectly referred to as the "modulus" operator. It is very similar to modulus, but does not work properly with negative numbers.

var remainder = 11 % 3;
Only change code below this line

var remainder;

Compound Assignment With Augmented Addition

Compound Assignment With Augmented Subtraction


Like the += operator, -= subtracts a number from a variable.

myVar = myVar - 5;

will subtract 5 from myVar. This can be rewritten as:

myVar -= 5;

Compound Assignment With Augmented Multiplication


The *= operator multiplies a variable by a number.

myVar = myVar * 5;

will multiply myVar by 5. This can be rewritten as:

myVar *= 5;

Compound Assignment With Augmented Division

There are no comments on this page.