KEMBAR78
JavaScript Functions | PPTX
JavaScript Functions
Simple Example
It defines a function called sayHello that takes no parameters
• Calling a Function
• To invoke a function somewhere later in the script, you would simply
need to write the name of that function as shown in the following
code.
Function Invocation
The code inside the function will execute when
"something" invokes (calls) the function:
When an event occurs (when a user clicks a button)
When it is invoked (called) from JavaScript code
Automatically (self invoked)
Function Return
The code inside the function will execute when
"something" invokes (calls) the function:
When an event occurs (when a user clicks a button)
When it is invoked (called) from JavaScript code
Automatically (self invoked)
Why Functions?
You can reuse code: Define the code once, and use it many times.
You can use the same code many times with different arguments, to
produce different results.
The () Operator Invokes the Function
toCelsius refers to the function object, and toCelsius() refers to the
function result.
Accessing a function without () will return the function object instead
of the function result.
String in Javascript
 The JavaScript string is an object that represents a sequence of
characters.
 There are 2 ways to create string in JavaScript
 By string literal
 By string object (using new keyword)
1) String Literal
 The string literal is created using double quotes.
 Syntax
var stringname="string value";
<html>
<body>
<script>
var str="This is string literal";
document.write(str);
</script>
</body>
</html>
2) By string object (using new keyword)
Syntax
var stringname=new String("string literal");
Here, new keyword is used to create instance of string.
<html>
<body>
<script>
var stringname=new String("hello javascript string");
document.write(stringname);
</script>
</body>
</html>

JavaScript Functions

  • 1.
  • 2.
    Simple Example It definesa function called sayHello that takes no parameters • Calling a Function • To invoke a function somewhere later in the script, you would simply need to write the name of that function as shown in the following code.
  • 3.
    Function Invocation The codeinside the function will execute when "something" invokes (calls) the function: When an event occurs (when a user clicks a button) When it is invoked (called) from JavaScript code Automatically (self invoked)
  • 4.
    Function Return The codeinside the function will execute when "something" invokes (calls) the function: When an event occurs (when a user clicks a button) When it is invoked (called) from JavaScript code Automatically (self invoked)
  • 5.
    Why Functions? You canreuse code: Define the code once, and use it many times. You can use the same code many times with different arguments, to produce different results.
  • 6.
    The () OperatorInvokes the Function toCelsius refers to the function object, and toCelsius() refers to the function result. Accessing a function without () will return the function object instead of the function result.
  • 7.
    String in Javascript The JavaScript string is an object that represents a sequence of characters.  There are 2 ways to create string in JavaScript  By string literal  By string object (using new keyword)
  • 8.
    1) String Literal The string literal is created using double quotes.  Syntax var stringname="string value"; <html> <body> <script> var str="This is string literal"; document.write(str); </script> </body> </html>
  • 9.
    2) By stringobject (using new keyword) Syntax var stringname=new String("string literal"); Here, new keyword is used to create instance of string. <html> <body> <script> var stringname=new String("hello javascript string"); document.write(stringname); </script> </body> </html>