Thursday 28 February 2013

JavaScript : String Operators


String Concatenation

When working with JavaScript strings sometimes you need to join two or more strings together in to a single string. Joining multiple strings together is known as concatenation.

The concatenation operator

The concatenation operator (+) concatenates two or more string values together and return another string which is the union of the two operand strings.

The shorthand assignment operator += can also be used to concatenate strings.

Example :

The following web document demonstrates the use of concatenation operator (+) and shorthand assignment operator +=.

HTML Code

  1. <!doctype html>  
  2. <head>  
  3. <meta charset="utf-8">  
  4. <title>JavaScript string operator example.</title>  
  5. <meta name="description" content="This document contains an example of JavaScript string operator"/>  
  6. </head>  
  7. <body>  
  8. <script src="javascript-string-operator-example1.js"></script>  
  9. </body><br></html>  

JS Code

  1. var newParagraph = document.createElement("p");   
  2. var newText = document.createTextNode("'google' + '.' + ' com=" + 'google' + '.' + ' com');  
  3. newParagraph.appendChild(newText);   
  4. document.body.appendChild(newParagraph);   
  5.   
  6. var string1= 'google';  
  7. var newParagraph1 = document.createElement("p");   
  8. var newText1 = document.createTextNode('string1='+string1);  
  9. newParagraph1.appendChild(newText1);  
  10. document.body.appendChild(newParagraph1);  
  11.   
  12. string1 +='.com';  
  13. var newParagraph1 = document.createElement("p");   
  14. var newText1 = document.createTextNode('string1='+string1);  
  15. newParagraph1.appendChild(newText1);  
  16. document.body.appendChild(newParagraph1);   

Practice the example above online






JavaScript : Conditional Operator

?: (Conditional operator)

The conditional operator is used as a shortcut for standard if statement. It takes three operands.

Syntax

Condition ? expr1 : expr2

Parameters

condition : An expression that evaluates to true or false.
expr1, expr2 : Expressions with values of any types.
If condition is true, the operator returns the value of expr1; otherwise, it returns the value of expr2.
For example
status = (marks >= 30) ? "Pass" : "Fail"
The statement assigns value "Pass" to the variable status if marks is 30 or more. Otherwise it assigns the value of "Fail" to status.

Example :

In the following web document the conditional operator statement [status = (marks >= 30) ? "Pass" : "Fail"] assigns value "Pass" to the variable status if marks is 30 or more. Otherwise it assigns the value of "Fail" to status.

HTML Code

  1. <!doctype html><head>  
  2. <meta charset="utf-8">  
  3. <title>JavaScript conditional operator example with DOM</title>  
  4. <meta name="description" content="This document contains an example of JavaScript conditional operator with DOM" />  
  5. </head>  
  6. <form name="example" onsubmit="ViewOutput()">  
  7. <input type="text" id="marks" placeholder="Enter Marks" />  
  8. <input type="submit" value="Submit" />  
  9. </form>  
  10. <body>  
  11. <script src="javascript-conditional-operator-example1.js">  
  12. </script>  
  13. </body>  
  14. </html>  
  15.    

JS Code

  1. function ViewOutput()  
  2. {  
  3. 'use strict';  
  4. var marks = document.getElementById("marks").value;  
  5. var status1 = (marks >= 30) ? "Pass" : "Fail";  
  6. var newParagraph = document.createElement("p"); //creates a new paragraph element  
  7. var newText = document.createTextNode(status1); //creates text along with ouput to be displayed   
  8. newParagraph.appendChild(newText); //created text is appended to the paragraph element created  
  9. document.body.appendChild(newParagraph); // created paragraph and text along with output is appended to the document body  
  10. }  

JavaScript : Conditional Operator and If else statement

The conditional operator statement of the above example
status = (marks >= 30) ? "Pass" : "Fail" is equivalent to the following statement.
  1. if marks>=30  
  2. document.write("Pass");  
  3. else  
  4. document.write("Fail");  

No comments:

Post a Comment