`
schy_hqh
  • 浏览: 542569 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

w3r-javascript06: Statement

 
阅读更多

 

Block statement

 var a = 101
  {
  a = 1;
  }
  alert(a) 
  //output 1

 

 

 

Conditional Statements

if...else

Executes a group of statements if a logical condition is true. Use the optional else clause to execute another group of statements.

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>JavaScript if  else statement :  Example-1</title>
</head>
<body>
<h1 style="color: red">JavaScript : if else statement</h1>
<h3>Here the if  else statement check whether the input marks is greater than 50 or not.</h3>
<hr />
<form name="form1" action ="#">
Input the marks<input type="text" name="text1" value=" " />
<input type="button" value="Marks check"
onclick='marksgrade()' />
</form>
<script src="if-else-example1.js"></script>
</body>
</html>

 

function marksgrade()
{
if (document.form1.text1.value>50)
alert('Marks is greater than 50.');
else
alert('Marks is less than or equal to 50.');
}

 

 switch

 

Syntax

switch (expression){

  case label :

    statements;

    break;

  case label :

    statements;

    break;

 ... default : statements;

}

 

Parameters

expression : Value matched against label.

label : An Identifier to be matched against expression.

statements : Group of statements that are executed once if expression matches label.

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>JavaScript Switch statement : Example-1</title>
<link rel="stylesheet" type="text/css" href="example.css">
</head>
<body>
<h1>JavaScript : switch statement</h1>
<form name="form1" action ="#">
Input Grade type : <input type="text" name="text1" value="A" />
<br /><br />
<input type="button" value="Marks check"
onclick='marksgrade()' />
</form>
<script src="switch-statement-example1.js"></script>
</body>
</html>

  

function marksgrade()
{
grade = document.form1.text1.value;
switch (grade)
{
case 'A+':
alert("Marks >= 90");
break;
case 'A':
alert("Marks [ >= 80 and <90 ]");
break;
case 'B+':
alert("Marks [ >= 70 and <80 ]");
break;
case 'B':
alert("Marks [ >= 60 and <70 ]");
break;
case 'C':
alert("Marks < 60");
break;
default:
alert("Wrong grade.........");
}
}

 

 

 

Loop Statements

do while

while

for

break

continue

 

Object Manipulation Statements

for ... in

Iterates a specified variable over all the properties of an object

and execute one or more statements for each property of the object. 

 

for (variable in object)

 

{

statements

}

 

Variable : Variable to iterate over every property of the object and the variable is accessible outside the loop, after completing the loop.

statements : The statement to be executed for each property of an object. For multiple statements within the loop use a block statement ({..}). 

 

 

function demo()
{
var  key, str1 = "";
// Initialize object.
var student = {
name : "David Rayy",
classname : "V",
rollno : 12
};
// Iterate the properties.
for(key in student)
{
str1 = str1+ student[key];
}
return(str1);
}
 

 

 

Exception Handling Statement

try...catch...throw

window.onload = function() {
		var empcode = prompt(
				"Input the Employee code : (Between 3 to 8 characters):", "");
		try {
			if (empcode.length > 8) {
				throw "error1";
			} else if (empcode.length < 3) {
				throw "error2";
			}
		} catch (err) {
			if (err == "error1") {
				alert("The Employee code length exceed 8 characters.");
			}
			if (err == "error2") {
				alert("The Employee code length is less than 3 characters");
			}
		}
	}

 

 

 

Other Statements

 

return expression

The return statement returns a value and exits from the current function. 

expression : The expression to return. If not present, function does not return a value. 

 

const

A constant is an identifier for a simple value. The value cannot be modified during the script's execution. 

The current implementation of const is a Mozilla-specific extension and is not part of ECMAScript 5. 

 

Syntax

const varname1 = value1 , varname2 = value2,... varnameN = valueN 

 

const height = 400;
const width = 200;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics