string var usernamesmith Boolean it has only two values

String var usernamesmith boolean it has only two

This preview shows page 33 - 52 out of 137 pages.

//string /* var username=“smith"; document.write(username);*/ Boolean: it has only two values. It test for something and give the result as true or false
Image of page 33
// string concatenation var sentence1= "My name is Tom"; var sentence2= " and i am 21 years old"; var sentence3; sentence3=sentence1+sentence2; //document.write(sentence3); //string concatenation with numbers var age=21; document.write(sentence1+" and i am "+age+" years old"); //using HTML elements inside our string document.write("<h1>Hello World</h1>") </script>
Image of page 34
Example: Finding Length of the String <html> <head> </head> <body> <script language="javascript"> var name; name= prompt("enter your name"); document.writeln("string length is "+ name.length ); document.close(); </script> </body> </html>
Image of page 35
OUTPUT string length is 7
Image of page 36
Image of page 37
Image of page 38
Image of page 39
Image of page 40
escape character( \ ) /* var status="Billy said \ "javascript is the most interesting client side scripting language\"" ; document.write(status); */ escape character in javascript just ignore the character immediately after a slash. javaScript is Case Sensitive All JavaScript identifiers are case sensitive . The variables lastName and lastname , are two different variables.
Image of page 41
Image of page 42
Functions A JavaScript function is a block of code designed to perform a particular task. function functionname () { some code to be executed } // with arguments function functionname ( par1 , par2 ) { some code to be executed }
Image of page 43
Example – Function Definition <script > function sayHello() { document.write("Hello there"); } </script> Calling a Function: <script > sayHello(); </script>
Image of page 44
Functions With Arg& Return Value Function Declaration <script > function concatenate(first, last) { var full; full = first + last; return full; } </script> Function Calling <script > var result; result = concatenate('Zara', 'Ali'); document.write(result ); </script>
Image of page 45
Example : <html> <head> <script> function sum(num1, num2) //Function Definition { var s=(num1)+(num2); document.write("addition="+s); } </script> </head> <body> <script> sum(3,4); </script></ body> // Function Calling </html>
Image of page 46
With return type <html> <body> <script> function sum() //Function Definition { var num1=prompt("enter 1st no"); var num2=prompt("enter 2nd no"); var s=parseInt(num1)+parseInt(num2); return s; } var result=sum(); // Function Calling document.write(result); </script> </body> </html>
Image of page 47
Calling function inside Table from HTML <html> <head> <script > function sayhello() { document.write("I am saying hello"); } </script> </head body> <script> document.writeln("<html> <head>HI</head><body>"); document.writeln("<h1> welcome</h1>"); document.writeln("<table> <tr><td>First Time"); sayhello(); document.writeln("</td></tr>"); document.writeln("<tr><td> Second Time"); sayhello(); </script> </body> </html>
Image of page 48
OUTPUT: HI welcome First Time I am saying hello Second Time I am saying hello
Image of page 49
Scoping rules Programming language usually impose rules called scoping, which determines how a variable can be accessed. 1. global variable 2.local variable
Image of page 50
JavaScript Scope Scope is the set of variables you have access to. Local JavaScript Variables Variables declared within a JavaScript function, become LOCAL to the function. Local variables have local scope : They can only be accessed within the function.
Image of page 51
Image of page 52

You've reached the end of your free preview.

Want to read all 137 pages?

  • Spring '20
  • Document Object Model

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes