Saturday, March 30, 2013

Start JavaScript

2. Data types

a. numbers (e.g. 4.3, 134)

b. strings (e.g. "dogs go woof!","JavaScript expert")

c. booleans (e.g. false, 5 > 4)
3. Conditionals

If the first condition is met, execute the first code block. If it is not met, execute the code in theelse block. See the code on the right for another example.

Box pop up:
Alert("message");  showing error message with ok button
confirm("message");  questioning for proceed further with ok and cancel button
prompt("message");  asking for user input with ok and cancel button

console.log("message");

Three Data Type:
Number: without double quotation like 5
String: within double quotation like "string"
Boolean: true/false like 5>2

PART 2

Variables
We store data values in variables. We can bring back the values of these variables by typing the variable name.
Manipulating numbers & strings

a. numbers - comparison operators, modulo
b. strings - length, substring
console.log( ) 
Prints into the console whatever we put in the parentheses.



"string".length


Substring
"some word".substring(x, y) where x is where you start chopping and y is where you finish chopping the original string. string index start from zero. examples:

1. First 3 letters of "Batman"
"Batman".substring(0,3): Bat

2. From 4th to 6th letter of "laptop"
"laptop".substring(3,6) : top

Modulo and if/else
if( 14 % 2 == 0 ) {
    console.log("The first number is even!");
} else {
    console.log("The first number is odd!");
}


Variables
we need a way to 'save' the values from our coding. We do this by defining a variable with a specific, case-sensitive name. Once you create (or declare) a variable as having a particular name, you can then call up that value by typing the variable name.
Code:

var varName = data type;
Example:

a. var myName = "Leng";
b. var myAge = 30;
c. var isOdd = true;


No comments:

Post a Comment