JS 02: JavaScript Syntax
Javascript codes are written inside the <script> tag. The <script> tag usually placesd inside the head section of the HTML page. General syntax is:
The JavaScript codes goes in between the <script> ... </script> tags.
Lets write a hello world program using javascript
The document.write() function takes a string as parameter and display the string in browser. This example code will simple display the "hello world" message:
hello world
To display the same message when user clicked a button:
The second example is how function is written in JavaScript. This function is called when onclick event happen, This event is triggered when user clicked the button. variables in java script is declared using var keyword. this does not differentiate number, string or Boolean data type.
JS supports arithmetic, comparison, logical, assignment and conditional operators
if else in javascript
</script>
the switch
while loop
do while loop
for loop
sending argument to function
Dialog boxes
We have used alert dialog box in previous examples, alert box is for showing warnings messages. Other type of dialog boxes are confirmation box, which takes yes or no as answer and another one is to take inputs from users, confirm() and prompt() respectively.
<script>
alert(“Hi this is a message using alert.”);
</script>
this id the confirm
for the prompt
<script type>
<!—javascript codes are written here -->
</script>
<!—javascript codes are written here -->
</script>
The JavaScript codes goes in between the <script> ... </script> tags.
Lets write a hello world program using javascript
<html>
<head>
<script language = "javascript" type = "text/javascript">
document.write("hello world");
</script>
</head>
<body>
</body>
</html>
<head>
<script language = "javascript" type = "text/javascript">
document.write("hello world");
</script>
</head>
<body>
</body>
</html>
The document.write() function takes a string as parameter and display the string in browser. This example code will simple display the "hello world" message:
hello world
To display the same message when user clicked a button:
<html>
<head>
<script language = "javascript" type = "text/javascript">
function DisplayGreeting(){
alert(“Hello world”);
}
</script>
</head>
<body>
<input type = “button” onclick = “displayGreetings()” value = “Click me”/>
</body>
</html>
<head>
<script language = "javascript" type = "text/javascript">
function DisplayGreeting(){
alert(“Hello world”);
}
</script>
</head>
<body>
<input type = “button” onclick = “displayGreetings()” value = “Click me”/>
</body>
</html>
The second example is how function is written in JavaScript. This function is called when onclick event happen, This event is triggered when user clicked the button. variables in java script is declared using var keyword. this does not differentiate number, string or Boolean data type.
<script>
var name;
var age = 23;
name = “Owen”;
document.write(name);
</script>
var name;
var age = 23;
name = “Owen”;
document.write(name);
</script>
JS supports arithmetic, comparison, logical, assignment and conditional operators
<scipt>
var num1 = 21;
var num2 = 21;
var total = num1 + num2;
document.write(total);
</script>
var num1 = 21;
var num2 = 21;
var total = num1 + num2;
document.write(total);
</script>
if else in javascript
<script>
var phone = “samsung”;
if(phone == “iphone”){
document.write(“Phone is iPhone”);
}else if(phone == “samsung”);
document.write(“Phone is Samsung”);
}else{
document.write(“Unknown phone brand”);
}</script>
the switch
<script>
switch(number)
{
case 1: document.write(“One”);
break;
case 2: document.write(“Two”);
break;
case 3: document.write(“Three”);
break;
case 4: document.write(“Four”);
break;
default:document.write(“Outside range of 1 to 4”);
}
</script>
switch(number)
{
case 1: document.write(“One”);
break;
case 2: document.write(“Two”);
break;
case 3: document.write(“Three”);
break;
case 4: document.write(“Four”);
break;
default:document.write(“Outside range of 1 to 4”);
}
</script>
while loop
<script>
var num = 0;
while(num < 5){
document.write(“Num : “ + num +”<br/>”);
num = num + 1;
}
</script>
var num = 0;
while(num < 5){
document.write(“Num : “ + num +”<br/>”);
num = num + 1;
}
</script>
do while loop
<script>
var num = 0;
do{
document.write(“Num : “ + num +”<br/>”);
num = num + 1;
} while(num < 5);
</script>
var num = 0;
do{
document.write(“Num : “ + num +”<br/>”);
num = num + 1;
} while(num < 5);
</script>
for loop
<script>
var num ;
for(num = 0; num < 5; num++){
document.write(“Num : “ + num + ”<br/>”);
}
</script>
var num ;
for(num = 0; num < 5; num++){
document.write(“Num : “ + num + ”<br/>”);
}
</script>
sending argument to function
<head>
<script>
function greet(name){
alert(“hello “ + name + “, welcome!!”);
}
</script>
</head>
<body>
<input type = “button” onclick= “greet(‘Owen’)” value = “Click me”/>
</body>
<script>
function greet(name){
alert(“hello “ + name + “, welcome!!”);
}
</script>
</head>
<body>
<input type = “button” onclick= “greet(‘Owen’)” value = “Click me”/>
</body>
Dialog boxes
We have used alert dialog box in previous examples, alert box is for showing warnings messages. Other type of dialog boxes are confirmation box, which takes yes or no as answer and another one is to take inputs from users, confirm() and prompt() respectively.
<script>
alert(“Hi this is a message using alert.”);
</script>
this id the confirm
<script>
var userResponse = confirm("Confirm yes or no?");
if(userResponse == true){
alert("You pressed yes");
}else{
alert("you pressed no");
}
</script>
var userResponse = confirm("Confirm yes or no?");
if(userResponse == true){
alert("You pressed yes");
}else{
alert("you pressed no");
}
</script>
for the prompt
<script>
var userInput = prompt("What is your name?");
alert("Hi, " + userInput + ", welcome");
</script>
var userInput = prompt("What is your name?");
alert("Hi, " + userInput + ", welcome");
</script>
Comments
Post a Comment