Functions, Global Functions, and Custom Objects.
Functions, Global Functions, and Custom Objects.
Rating:
The syntaxes for creating a function are as follows:
{
//statements
}
The above syntax of creating the function does not contain any parameter.
{
//statements
}
The above syntax of creating the function contains parameters. A function can have any number of parameters that should be passed in its parentheses.
The function name should begin with an alphabet or underscore. The name of a function cannot begin with a number. This will result in an error. Numbers can only be used in the middle of the function name. The keywords and the special characters such as <, >, period comma, etc, cannot be used in the function name because these are reserved in JavaScript. The functions are invoked by the event handlers of an object.
The syntax of invoking a function is as follows:
<input type="button" onclick="functionname()">
</form>
The function should be in the same case (uppercase or lowercase) as it has been defined in the script.
The following example will demonstrate the creation and the invocation of a function:
<head>
<script language="javascript">
function myfunc()
{
document.write("This is a function")
}
</script>
</head>
<body>
<form>
<input type="button" value="Invoke Function" onclick="myfunc()">
</form>
</body>
</html>
The syntax of using the eval function is as follows:
The following example will demonstrate the use of the eval function:
function evaldemo()
{
eval("document.write('This is an example of eval function')")
}
</script>
The eval function can also be used to convert a string to a number. The following example will demonstrate this:
function evaldemo()
{
var a=eval(prompt("enter a number"))
var b=eval(prompt("enter a number"))
var c=a+b
alert(c)
}
</script>
The syntax of using the escape function is as follows:
The following example will demonstrate the use of the escape function:
function escdemo()
{
var a="hello world"
var b=escape(a)
document.write(b)
}
</script>
The syntax of using the isNaN function is as follows:
The following example will demonstrate the use of the isNaN function:
function isnandemo()
{
var a=prompt("Enter a number")
if(isNaN(a))
{
alert("This is not a number")
}
else
{
document.write(a)
}
}
</script>
The syntax of using the Number function is as follows:
The following example will demonstrate the use of the Number function:
function numdemo()
{
var a=Number(prompt("Enter a number"))
var b=Number(prompt("Enter a number"))
var c=a+b
alert(c)
}
</script>
The parseInt function is used to convert a string value to a numeric value. If the string value does not contain numbers, it returns NaN (Not a Number).
The syntax of using the parseInt function is as follows:
The following example will demonstrate the use of the parseInt function:
function numdemo()
{
var a=parseInt(prompt("Enter a number"))
var b=parseInt(prompt("Enter a number "))
var c=a+b
alert(c)
}
</script>
The parseFloat function is used to convert a string value to a numeric value. If the string value does not contain numbers, it returns NaN (Not a Number).
The syntax of using the parseFloat function is as follows:
The following example will demonstrate the use of the parseFloat function:
function numdemo()
{
var a=parseFloat(prompt("Enter a number"))
var b=parseFloat(prompt("Enter a number "))
var c=a+b
alert(c)
}
</script>
The syntax of using the String function is as follows:
The following example will demonstrate the use of the String function:
function strdemo()
{
var a=Number(prompt("enter a number"))
var b=Number(prompt("enter a number"))
var c=a+b
alert(c)
var d=String(a)
var e=String(b)
var f=d+e
alert(f)
}
</script>
The syntax of using the toString function is as follows:
The radix argument is optional. The radix argument accepts the values from 0 to 16 and converts the number according to binary, decimal, hexadecimal, or to another base that is passed as the value of radix.
The following examples will demonstrate the use of the toString function;
function tostrdemo()
{
var arr=new Array(1,2,3,4,5)
var str=arr.toString()
alert(str)
}
</script>
The above program will convert the elements of an array to string.
function radixdemo()
{
var a=10
var b=a.toString(2)
alert(b)
}
</script>
The above program will convert the value of the variable a to its binary equivalent.
The syntax of using the unescape function is as follows:
function unescdemo()
{
var a="hello%20world"
var b=unescape(a)
document.write(b)
}
</script>
The syntax of using the properties and methods of an object are as follows:
object.methodname
JavaScript contains the objects that can be used for various purposes such as window, document, form, etc.
A user can create his own objects in JavaScript that can have custom properties and custom methods. These kinds of objects are called user-defined objects or custom objects.
The code for creating a custom object is as follows:
function Student(name,age,address,getName)
{
this.name=name //Property
this.age=age //Property
this.address=address //Property
this.getName=getName //Method
}
</script>
The code for using the custom object is as follows:
function useobj()
{
var stu=new Student("John", 20, "California")
alert(stu.name)
}
</script>
The code for the getName method is as follows:
function getName(name)
{
this.name=name
return name
}
</script>
Rating:
Other articles
- What is the port property?
- What is VBScript?
- What is the write method?
- What is the hidden object?
- What is a script?
