GTU WTAD Practical 2 : Write a JavaScript that demonstrates the use of +=,-=,*=,/= operators

GTU WTAD Practical 2 : Write a JavaScript that demonstrates the use of +=,-=,*=,/= operators

GTU WTAD Practical 2

This program demonstrate use of JavasScript advance operator. JavaScript supports all operator which are supported by general purpose language. But here in this example I am going to discuss the special operator supported by JavaScript that is +=, -=, *=, and /=. This operator is actually combinations of two operators, lets learn by example.

Jump to GTU WTAD Example 3: Create a Form in HTML.

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu02\

<html>
<head>
	<title> GTU WTAD Practical 2 : Write a JavaScript that demonstrates the use of +=,-=,*=,/= operators </title>

	<script language="javascript">
		function gtu_prog2()
		{
			var a=100,b=50;
			document.write("<br>Values of Variable A : " + a +" and B is : " + b + "<br>");
			document.write("<hr>");
			document.write("<b>1. a+=b : "+(a+=b)+"</b><br>");
			var a=10,b=5;
			document.write("<b>2. a-=b : "+(a-=b)+"</b><br>");
			var a=10,b=5;
			document.write("<b>3. a*=b : "+(a*=b)+"</b><br>");
			var a=10,b=5;
			document.write("<b>4. a/=b : "+(a/=b)+"</b><br>");
			document.write("<hr>");

		}
	</script>
</head>
<body>
	<center> <h2> GTU WTAD Practical 2 : Write a JavaScript that demonstrates the use of +=,-=,*=,/=(shorthand) operators . </h2></center>
	<script language="javascript">
		gtu_prog2()
	</script>
	
</body>
</html>

 

Output

GTU WTAD Practical 2

 

If you are not sure about how to write JavaScript or basic fundamental points for JavaScript, I strongly recommend you to refer my previous post regarding JavaScript to learn basic about JavaScript.

 

Few Important JavaScript Keywords, DO NOT USE IN VARIABLE NAME OR FUNCTION NAME

  • var : This Keyword is used to declare variable in JavaScript.
  • for : This JavaScript Keyword is for Looping Structure.
  • do : This  JavaScript Keyword is for Looping Structure.
  • while : This  JavaScript Keyword is for Looping Structure.
  • break : This JavaScript Keyword is used to break loop on some special condition.
  • continue : This JavaScript Keyword is used for Looping Structure to skip some condition.
  • switch : This JavaScript Keyword is used to write down switch instead of if… else condition.
  • function : This JavaScript keyword is used to declare function in JavaScript.
  • try : This JavaScript keyword is for Exception handling.
  • catch : This JavaScript keyword is for Exception Handling.
  • return : This JavaScript keyword is used to return from function.

 

Whats Default value for Variable in JavaScript ?

undefined (like null in some other languages)

 

Suppose you re-declare same variable will you lost your existing value ?

No

 

var myname = “Adarsh” is a string typeof variable ?

Yes

 

var mymark=100 is a number type of variable ?

Yes

 

var mymark=”100″ is a number type of variable ?

NO. (Its considered as string)

 

How to combine two string in JavaScript ?

<script>
     var name="Adarsh " + "Patel";
     document.write(name);
</script>

 

Whats Difference between using single quote for String and Double quote for string ?

Both are Same (In double quote string you can use apostophe s in between)

 

How many DataTypes are Supported by JavaScript ?

5 (Text,Number, Boolean, Array and Object)

 

How to Declare variable in All Supported Types in JavaScript?

<script>

var v1 = 1;
var v2 = 1.5;
var v2 = "Adarsh Patel";
var v3 = true;
var v4 = ["Java", "JavaScript", "PHP"];
var v5 = {fname:"Adarsh", lname:"Patel"};

document.write(v1);
document.write("<br/>");
document.write(v2);
document.write("<br/>");
document.write(v3);
document.write("<br/>");
document.write(v4[1]);
document.write("<br/>");
document.write(v5.fname);
document.write("<br/>");

/*
    Output : 

1
Adarsh Patel
true
JavaScript
Adarsh 

*/
</script>

 

How to call/identify function in JavaScript ?

() is used to call function

 

How to find length of string in JavaScript ?

<script>
     var name="Adarsh Patel";
     document.write(name.length);
</script>

 

How to display special character in JavaScript ?

use \ (escape sequence)

<script>
     document.write("Thank\'s for reading my blog");
</script>

 

Where can we use JavaScript ?

  • on click of submit button for form validation
  • on click of login button for blank text validation
  • on text change of input type text to validate text
  • on page loading completes or other binding with page

 

Some of Useful String Function of JavaScript(output is written in comment)

<script>
   var msg = "This is a sample text to test string methods"
   document.write(msg.charAt(8));          // a
   document.write(msg.charCodeAt(8));      // 97
   document.write(msg.indexOf("a"));       // 8
   document.write(msg.toLowerCase(msg));   // convert msg to lower case
   document.write(msg.toUpperCase(msg));   // convert msg to UPPER CASE
   document.write(msg.trim(msg));          // Remove extra space from staring and ending
   document.write(msg.replace("a","an"));  // This is an ...
</script>

 

 How to extract sub string from Main String in JavaScript ?

<script>
   var msg = "This is a sample text to test string methods"
   document.write(msg.slice(5,7));        // is
   document.write(msg.substring(5,7));    // is
   document.write(msg.substr(5,2));       // is
</script>

 

 

Number Conversion between Decimal, Binary,Octal and Hex in JavaScript

<script>
   var mynumber=15;
   document.write(mynumber.toString(2));  //Binary output (1111)
   document.write("<br/>"); 
   document.write(mynumber.toString(8));  //Octal output (17)
   document.write("<br/>");  
   document.write(mynumber.toString(16)); //Hex output (f)
</script>

 

 

 

 

Leave a Reply