Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

Programming fundamental using Javascript

----------------------------------------

Scripting languages are embedded into another programming language, like


actionscript in Flash, javascript in webbrowser or some of application called
Javascript is an interpreted language

In another term Javascript is a C based interpreted language

Javascript is a case sensitive language

Variables:
----------

Variables are simply containers

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

Note: numbers should not be at start


e.g. var 99prblems; ---- incorrect
var problems99; -- correct

Variable can be defined in two ways;

var year = 2000;


year = 2000; (javasript find the exsting variabe to drop if not it will make it)

Diff., variable can be defined as;

var year = 2000;


var month = 11;
var day = 30;

or

var year = 2000, month =11, day = 30;

*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.

Using equal sign in defining variable is otherwise called assignment operator

Strings:

Any value define within quotes is called strings

Single and double Quotes can be used to define strings but if start with single
quote and end should be single not double

Quotes inside Quotes;


var phrase = 'Don't mix your quotes.'; --- incorrect
var phrase = "Don't mix your quotes."; -- correct

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;

var result = (a+b);

result = 5 + 5 * 10; ----- 55

here multiplication would be done fist so if need to impose of order, simply


sorround them with parentheses;

result = (5 + 5) * 10; ----- 100

Adding a value to the existing variable;

score = score + 10;

or

score += 10;
for other operator as += -= *= /=

Incremen/Decrement with value 1 can be written 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:

Anything written after "//" will be converted as comment


otherwise multiple line comments written as:
/* comments
comments
*/
Conditional code:
-----------------

If statement format as;

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!");
// ...
}

Semicolon will not be used after at end braces

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

we can create multiple conditions as;

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;

var a = 123; // number


car b = "123"; // string

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;

if (a === b && c === d) { // if both condition are true then execute


if (a === b "insert two stright line" c === d) { // if any condition are true
then execute

can be also written as


if ( (a > b) && (c > d) ) { ....

Switch statement;

var grade = "Premium";

if (grade === "Regular") {


alert("It's $3.15");
}

if (grade === "Premium") {


alert("It's $3.35");
}

if (grade === "Diesel") {


alert("It's $3.47");
}

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;
}

Modular code (functions):


-------------------------

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();

we can call the same function for multiple times

You might also like