Some Basic Java Script

Hi guys, I thought of learning some Java Script as it has become one of the most important aspect of a programer in the present. So I thought of handling some basic default functions for a start. Hope you guys achieve some thing out from this post.

So here we will see how to write JavaScript inside the Basic html inside Body tag. We will see the external Java Script in another Tutorial.

So here we define the Script Tag inside the body and Write all the code explained between the Script Tags.

noscript tag is attached to inform people who are viewing this site in a browser that does not support Java Script.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>

<script language="javascript" type="text/javascript">

//Java Scrip Code Goes Here
</script>

<noscript><h3>This site needs Javascript Enabled Browser</h3></noscript>

</body>
</html>

So below are some basic Java Script code you can try yourself

1. A simple Alert

alert("Hi this is amalan");

2. Min and Max number supported by JS

document.write(Number.MAX_VALUE + "<br/>");
document.write(Number.MIN_VALUE + "<br/>");

3. Printing a String

var sampl_str = "Here are some Escape charecters \" \t \' \\ \n";
document.write(sampl_str + "</br>");

4. Combining Strings

var first_String = "First String";
var second_String = "Second String";
var combined_String = first_String + second_String ;
document.write(combined_String + "</br>");

5. Example of using substring function

document.write("Sub String " + combined_String.substring(12, 18) + "</br>");

6. Getting the Last character

document.write("Last Charecter =" + combined_String.charAt(combined_String.length-1) + "</br>");

7.Usage of Indexof function

document.write("Index of " + combined_String.indexOf('t') + "</br>");

8.Variables

var str_var = "5";
var num_var = 10;

var total = num_var + str_var;
var multy = num_var * str_var;

document.write("Total is :" + total+"</br>");
document.write("Multy is :" + multy+"</br>");

9.Force data type conversion

total = num_var + Number(str_var);
document.write("Total is :" + total+"</br>");

total = num_var + num_var;
document.write("Total is :" + total+"</br>");

total = num_var + String(num_var);
document.write("Total is :" + total+"</br>");

10. Float

var float_var = 3.1417376464646464;
var float_str = float_var.toFixed(7);
document.write("Shortened PI value by 7 : "+float_str+"</br>");

document.write("Data type of float_var = "+typeof(float_var)+"</br>");
document.write("Data type of float_str = " + typeof(float_str) + "</br>");

11.Boolean

var bool_var = Boolean(23);
document.write("Booloean of 23 is " + bool_var + "</br>");

12. Compare Variables

var rand_num1 = 5;
var rand_num2 = 10;

document.write("5 > 10 = "+(rand_num1 > rand_num2)+"</br>");
document.write("5 >= 10 = "+(rand_num1 >= rand_num2)+"</br>");
document.write("5 == 10 = "+(rand_num1 == rand_num2)+"</br>");
document.write("5 <= 10 = "+(rand_num1 <= rand_num2)+"</br>");
document.write("5 < 10 = "+(rand_num1 < rand_num2)+"</br>");
document.write("5 != 10 = "+(rand_num1 != rand_num2)+"</br>");

13.Logical operators

document.write("Is 5 < 10 and 5 != 10 "+((rand_num1 < rand_num2) && (rand_num1 != rand_num2))+"</br>");
document.write("Is 5 > 10 or 5 != 10 "+((rand_num1 > rand_num2) || (rand_num1 != rand_num2))+"</br>");
document.write("Is 5 < 10"+ !(rand_num1 < rand_num2)+"</br>");

 

14.Arrays

var vehihcals = new Array("Car", "Van","Bus");
document.write("Second Value of the Vehical Array is :" + vehihcals[1]+"</br>");

15.Cycle through Array

for (i in vehihcals) {

document.write("vehical value "+ (Number(i)+1)+" is "+vehihcals[i]+"</br>");

}

16.Conditional Opperators

var rand_var = (5 < 10) ? "5 is less than 10" : "5 is greater than 10";
document.write(rand_var + "</br>");

17.if Else

if (5 > 5) {
document.write("5 is greater than 10"+"</br>");
}

else if (5 < 5) {
document.write("5 is less than 10"+"</br>");
}
else {
document.write("Both are Equal"+"</br>");
}

18.Switch Case

var state = "Colombo";

switch (state) {
case "Kandy":
document.write("Colder than Me" + "</br>");
break;

case "Colombo":
document.write("Same as Me" + "</br>");
break;

default:
document.write("I dont know what you have entered"+"</br>");
}

19.loop

var count = 1;

while (count <= 10) {
document.write(count+"</br>");
count++;
}

20.Continue Statement

var count = 1;

while (count <= 10) {


//try break
//if (count == 6) {
//    break;
//}

if (count % 2) {
count++;
continue;
}

document.write(count + "</br>");
count++;
}

21.doWhile

var count = 10;

do {
document.write(count + "</br>");
count--;
} while (count > 10);

22.for

for (var count = 0; count <= 10; count++) {
document.write(count+"</br>");
}

 

Get the Code here : Download code

Reference : YouTube Reference

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s