Professional Documents
Culture Documents
Programming Fundamental Using Javascript
Programming Fundamental Using Javascript
----------------------------------------
Variables:
----------
Variable should be named to carry data, it has to be named as single word without
any space within it --- name can have letter, number, dollar sign and the
underscore other than no characters are allowed
or
*No need to define type of variable in Javascript which is otherwise known as weak
type language
Any type of value can be defined into a variable like interger, whole number,
decimel, string, boolean values and characters etc.
Strings:
Single and double Quotes can be used to define strings but if start with single
quote and end should be single not double
var phrase = "He said "that's fine," and left."; --- incorrect
var phrase = "He said \"that's fine,\" and left."; -- correct
If we need double quote in the string it should be marked with backslash before it.
Operators;
var a = 10;
var b = 5;
or
score += 10;
for other operator as += -= *= /=
a = a + 1;
a += 1;
a++;
Whitespaces:
Javascript doesn't care about whte spaces such as spaces, tabs, line breaks
Note:
i) Space to be needed between "var" and variable name
ii) No space required between two operators when combined
iii) Spaces within string are retained
Line break within string to be specify with "\n" precede to the breaking
Comments:
if (condition) {
// contional code goes here
// ...
}
Note: braces, brackets, parenthesis are all should have both start and end marking
to complete the statement
The value within the parenthesis is to be evaluated whether true or false like;
if (a > 10) {
// alert ("it's true!");
// ...
}
Note: to check whether condition is equal, we should use two equal sign since
single equal is use to assign the value
if (a == 10) {
alert ("it's true!");
// ...
}
if/else statement;
if (a >= 0) {
alert ("it's true!");
} else {
alert ("it's false!");
}
here the >= used to check both condition to avoid error if value is equal
if (a >= 0) {
alert ("it's true!");
if (a > 10) {
alert ("it's large!");
}
} else {
alert ("it's false!");
}
Comparison operator;
= is assignment
== is equality
=== is strictly equal
equaliy check;
if (a === b) {
alert("Yes, they are equal");
} else {
alert("No, they are not equal");
}
here three sign used to check equality between the number and string, if double
equal sign used the result will be true since it only matches the character instead
actual number
comparison;
if (a == b) { // equal check
if (a != b) { // inequal check
if (a === b) { // strictly equal check
if (a !=== b) { // strictly inequal check
if (a > b) { // ...
if (a < b) { // ...
if (a >= b) { // specified as one operator i.e. indivisible unit
if (a <= b) { // specified as one operator i.e. indivisible unit
Logical AND/OR;
Switch statement;
If need to check a variable for a selection of very specific values between range
of set values, we can use switch statement (select case statement) as;
var grade = "Premium";
switch (grade) {
case "Regular":
alert("It's $3.15");
break;
case "Premium":
alert("It's $3.35");
break;
case "Diesel":
alert("It's $3.47");
break;
default:
alert("That's not a valid grade"); // here default refers if not
anything do something else
break;
}
Breaking code apart into smaller, reusable, modular pieces these named as modules,
subroutines or subprogram or just routines or methods. In javascript it has been
called as functions
Function is simply taking block of code and giving it a name so that we can call it
later and treat this block of code as one thing. Functions named as verb-noun
format like calculateArea, createMeassage....
funtion myFunction() { // here the empty closed parenthesis can have data sometimes
alert("This code is inside the function");
}
//when made the function we've call to run otherwise it won't run;
myFuntion();