JavaScript Function in Hindi एक encapsulation की तरह होता है। जिसे आप कहीं भी या कभी भी run करवा सकते हो।
इसका मतलब यह है की आप Function में code को एक block में डिजाईन कर सकते हो,
और उससे कोई particular task perform करवा सकते हो।
लेकिन ऐसी तभी
होगा जब आप उस function को call करते हो।
अब शरू करने से
पहले जान लेते हैं की हमं इस tutorial में क्या-क्या पढने वाले हैं।
Introduction to JavaScript Function in Hindi
Syntax of Function
Example of Function
Introduction to JavaScript Function in Hindi
Function एक तरह से Code
का एक समूह होता है। जो की किसी विशेष task
को पूरा करने के लिए इस्तेमाल किया जाता है।
इसका use करके आप same ही code को बार-बार लिखने
की आवश्कता नहीं होती है।
यानि की आप एक
बार function को define
कर दो,
और जब भी आपको उस
code की आवश्कता होगी तो उसे call
करवा लो।
Function के जरिये आप एक
ही code को कई बार इतेमाल कर सकते
हो।
Function को कैसे define
किया जाता है?
JavaScript में Function
को function keyword द्वारा define किया जाता है।
उसके बाद आपको
इसमें function का नाम और उसके
बाद parentheses () का use करना होगा।
आप function
का नाम कोइ भी string, Number और sign द्वारा दे सकते हो।
यहां पर भी variable
के same rule apply होते हैं।
उसके बाद आपको curly
brackets { } लगाने हैं। अब इमें आप code
लिख सकते हो।
Syntax:
function function_name(arg0, arg1, ........., argN ){
// Code is Here
}
अब हमं एक Example
देख लेते हैं:
Code:
<!DOCTYPE html>
<html>
<head>
<title> JavaScript Function</title>
</head>
<body>
<p>Output:</p>
<p id="assig"></p>
<script type="text/javascript">
//This is Function
function func(name, message) {
document.write("Hello " + name+ " "+
message);
}
func("Rohit,", "How are you");//Calling
The Function and add values
</script>
</body>
</html>
Output:
hello rohit how r u
अब इस code
में हमनें सबसे पहले function को define किया है।
जसमें हमनें function
का नाम func रखा है।
वहीं parentheses
में हमने दो arguments name और message को पास किया है।
उसके बाद हमने curly
bracket{ } में document.write
द्वारा Output को show करवाया गया।
last में हमने function
को call करवाया func()
द्वारा।
यदि आप function
को call नहीं करते हो तो इसकी Output show नहीं होती है ।
पहले argument
में हमने name की value “Rohit” लिखा और दुसरे में हमनें message की value “How are You” लिखा।
Function Without arguments
Code:
<!DOCTYPE html>
<html>
<head>
<title> JavaScript Function Without
parentheses</title>
</head>
<body>
<p>Output:</p>
<p id="assig"></p>
<script type="text/javascript">
function func() {
document.write("Hello, What is your name?");
}
func();
</script>
</body>
</html>
Output
hello what is your name
JavaScript Function Return
जब भी आपको JavaScript
में return statement देखे तो,
इसका मतलब है की function
की execution वहीं stop हो जाती है।
और वह आपके सामने
Output को show कर देता है।
लेकिन वह output
उसी में जाती है जहाँ से function को call करवाया जाता है।
अब हमं इसे एक
उदाहरण से इसे समझ लेते हैं।
Code:
<!DOCTYPE html>
<html>
<head>
<title> JavaScript Function Return</title>
</head>
<body>
<p>Output:</p>
<p id="assig"></p>
<script type="text/javascript">
function func(num1,num2) {
return num1+num2;
}
var result = func(10, 20);//Function is called, the value of
return store here
document.write(result);
</script>
</body>
</html>
Output:30
अब इस code
में हमने function में दो arguments को पास किया है।
पहला argument
num1 और दूसरा argument
num2 है।
अब function
के अंदर हमने return लिखा और जो दोनों arguments की value होगी उसे plus
किया जाएगा।
अब जैसे की हमने function
को call किया और 10, 20 को हमने num1 और num2 की value execute कर दी।
last में जो solve
करके value आएगी वह variable result में store हो जाएगी।
और document.write
द्वारा आपकी output web page में show हो जाएगी।
Function का use क्यों किया जाता है?
इसका use करके आप code के size को reduce
कर सकते हो।
Function के जरिये आप Code
को reuse कर सकते हो।
इसके जरिये आप code
को छोटे-छोटे parts में divide कर सकते हो।
यदि कोइ error
भी आता है तो उसे आसानी से find किया जा सकता है।
आपको एक ही code
को बार-बार लिखने की आवश्कता नहीं होती है।
Example of Function
आप जतनी practice
करें उतना ही कम हैं।
क्युकी यदि आप Web
Development/ Web Designing field में आ रहें हैं
तो मेहनत को करनी होगी।
अब कुछ ओर Function
के example देख लेते हैं।
पहले हमं code
को perform करेंगे फिर उसे समझने की कोशिश करेंगे।
Example 1:
Code:
<!DOCTYPE html>
<html>
<head>
<title> JavaScript Function</title>
</head>
<body>
<p>Output:</p>
<p id="assig"></p>
<script type="text/javascript">
function func(num1,num2) {
return num1+num2;
}
document.getElementById("assig").innerHTML=
func(10, 12);//Using Inner HTML and calling function
</script>
</body>
</html>
Output:
22
अब यहां पर हमने Output
को Show करवाने के लिए Inner HTML का use किया है।
func(10, 12); यानिकी num1 में 10 और num2 में 12
value को store करवाया है।
उसके बाद इसकी value
को add किया गया,
और Output
की जो value आई वह paragraph की ID “assign” में show हो रही है।
Example 2:
अब हमं code
के साथ खेलना शरू करते हैं। मानलीजिये यदि हमं
जब last में function को call करते हैं तो,
उस में से () ना
लगायें तो क्या होगा?
Code:
<!DOCTYPE html>
<html>
<head>
<title> JavaScript Function</title>
</head>
<body>
<p>Output:</p>
<p id="assig"></p>
<script type="text/javascript">
function func(num1,num2) {
return num1+num2;
}
document.getElementById("assig").innerHTML=
func;//Using Inner HTML
</script>
</body>
</html>
Output:
अब जैसे की आप
देख सकते हो की हमने function को call करते समय func() ना लिख कर,
func ही लिखा है। तो
इसका result आपके सामने है।
JavaScript Local Variable in Function
यदि आप variable
को local बनाना चाहते हो तो function के द्वारा किया जा सकता है।
Local Variable का यह लाभ है की
आप इसे उसी function में ही access
कर सकते हो।
Code:
<!DOCTYPE html>
<html>
<head>
<title> JavaScript Local Function</title>
</head>
<body>
<p>Output:</p>
<p id="demo1"></p>
<p id="demo2"></p>
<script type="text/javascript">
localfunc();
function localfunc(){
var Name = "Rohan";
document.getElementById("demo1").innerHTML=
Name;//when local variable access from inside the function
}
document.getElementById("demo2").innerHTML= typeof
Name;// When local variable access from outside the function
</script>
</body>
</html>
Output:rohit
undifined
यहाँ हमें यह पता
चल रहा है की जो variable function के अंदर है उसे
सिर्फ उसी function से access
किया जा सकता है।
यदि हमं function
के बाहर से access करने की कोशिश करते हैं तो variable को undefined show हो रहा है।
JavaScript Built-in Functions
Number Methods
The Number object contains
only the default methods that are part of every object's definition.
Sr.No. |
Method &
Description |
1 |
Returns the function that
created this object's instance. By default this is the Number object. |
2 |
Forces a number to
display in exponential notation, even if the number is in the range in which
JavaScript normally uses standard notation. |
3 |
Formats a number with a
specific number of digits to the right of the decimal. |
4 |
Returns a string value
version of the current number in a format that may vary according to a
browser's locale settings. |
5 |
Defines how many total
digits (including digits to the left and right of the decimal) to display of
a number. |
6 |
Returns the string
representation of the number's value. |
7 |
Returns the number's
value. |
Boolean Methods
Here is a list of each
method and its description.
Sr.No. |
Method &
Description |
1 |
Returns a string
containing the source of the Boolean object; you can use this string to
create an equivalent object. |
2 |
Returns a string of
either "true" or "false" depending upon the value of the
object. |
3 |
Returns the primitive
value of the Boolean object. |
String Methods
Here is a list of each
method and its description.
Sr.No. |
Method &
Description |
1 |
Returns the character at
the specified index. |
2 |
Returns a number
indicating the Unicode value of the character at the given index. |
3 |
Combines the text of two
strings and returns a new string. |
4 |
Returns the index within
the calling String object of the first occurrence of the specified value, or
-1 if not found. |
5 |
Returns the index within
the calling String object of the last occurrence of the specified value, or
-1 if not found. |
6 |
Returns a number
indicating whether a reference string comes before or after or is the same as
the given string in sort order. |
7 |
Returns the length of the
string. |
8 |
Used to match a regular
expression against a string. |
9 |
Used to find a match
between a regular expression and a string, and to replace the matched
substring with a new substring. |
10 |
Executes the search for a
match between a regular expression and a specified string. |
11 |
Extracts a section of a
string and returns a new string. |
12 |
Splits a String object
into an array of strings by separating the string into substrings. |
13 |
Returns the characters in
a string beginning at the specified location through the specified number of
characters. |
14 |
Returns the characters in
a string between two indexes into the string. |
15 |
The characters within a
string are converted to lower case while respecting the current locale. |
16 |
The characters within a
string are converted to upper case while respecting the current locale. |
17 |
Returns the calling
string value converted to lower case. |
18 |
Returns a string
representing the specified object. |
19 |
Returns the calling
string value converted to uppercase. |
20 |
Returns the primitive
value of the specified object. |
String HTML wrappers
Here is a list of each
method which returns a copy of the string wrapped inside the appropriate HTML
tag.
Sr.No. |
Method &
Description |
1 |
Creates an HTML anchor
that is used as a hypertext target. |
2 |
Creates a string to be
displayed in a big font as if it were in a <big> tag. |
3 |
Creates a string to blink
as if it were in a <blink> tag. |
4 |
Creates a string to be
displayed as bold as if it were in a <b> tag. |
5 |
Causes a string to be
displayed in fixed-pitch font as if it were in a <tt> tag |
6 |
Causes a string to be
displayed in the specified color as if it were in a <font
color="color"> tag. |
7 |
Causes a string to be
displayed in the specified font size as if it were in a <font
size="size"> tag. |
8 |
Causes a string to be
italic, as if it were in an <i> tag. |
9 |
Creates an HTML hypertext
link that requests another URL. |
10 |
Causes a string to be
displayed in a small font, as if it were in a <small> tag. |
11 |
Causes a string to be
displayed as struck-out text, as if it were in a <strike> tag. |
12 |
Causes a string to be
displayed as a subscript, as if it were in a <sub> tag |
13 |
Causes a string to be
displayed as a superscript, as if it were in a <sup> tag |
Array Methods
Here is a list of each
method and its description.
Sr.No. |
Method &
Description |
1 |
Returns a new array
comprised of this array joined with other array(s) and/or value(s). |
2 |
Returns true if every
element in this array satisfies the provided testing function. |
3 |
Creates a new array with
all of the elements of this array for which the provided filtering function
returns true. |
4 |
Calls a function for each
element in the array. |
5 |
Returns the first (least)
index of an element within the array equal to the specified value, or -1 if
none is found. |
6 |
Joins all elements of an
array into a string. |
7 |
Returns the last
(greatest) index of an element within the array equal to the specified value,
or -1 if none is found. |
8 |
Creates a new array with
the results of calling a provided function on every element in this array. |
9 |
Removes the last element
from an array and returns that element. |
10 |
Adds one or more elements
to the end of an array and returns the new length of the array. |
11 |
Apply a function
simultaneously against two values of the array (from left-to-right) as to
reduce it to a single value. |
12 |
Apply a function
simultaneously against two values of the array (from right-to-left) as to
reduce it to a single value. |
13 |
Reverses the order of the
elements of an array -- the first becomes the last, and the last becomes the
first. |
14 |
Removes the first element
from an array and returns that element. |
15 |
Extracts a section of an
array and returns a new array. |
16 |
Returns true if at least
one element in this array satisfies the provided testing function. |
17 |
Represents the source
code of an object |
18 |
Sorts the elements of an
array. |
19 |
Adds and/or removes
elements from an array. |
20 |
Returns a string
representing the array and its elements. |
21 |
Adds one or more elements
to the front of an array and returns the new length of the array. |
Date Methods
Here is a list of each
method and its description.
Sr.No. |
Method &
Description |
1 |
Returns today's date and
time |
2 |
Returns the day of the
month for the specified date according to local time. |
3 |
Returns the day of the
week for the specified date according to local time. |
4 |
Returns the year of the
specified date according to local time. |
5 |
Returns the hour in the
specified date according to local time. |
6 |
Returns the milliseconds
in the specified date according to local time. |
7 |
Returns the minutes in
the specified date according to local time. |
8 |
Returns the month in the
specified date according to local time. |
9 |
Returns the seconds in
the specified date according to local time. |
10 |
Returns the numeric value
of the specified date as the number of milliseconds since January 1, 1970,
00:00:00 UTC. |
11 |
Returns the time-zone
offset in minutes for the current locale. |
12 |
Returns the day (date) of
the month in the specified date according to universal time. |
13 |
Returns the day of the
week in the specified date according to universal time. |
14 |
Returns the year in the
specified date according to universal time. |
15 |
Returns the hours in the
specified date according to universal time. |
16 |
Returns the milliseconds
in the specified date according to universal time. |
17 |
Returns the minutes in
the specified date according to universal time. |
18 |
Returns the month in the
specified date according to universal time. |
19 |
Returns the seconds in
the specified date according to universal time. |
20 |
Deprecated - Returns the year
in the specified date according to local time. Use getFullYear instead. |
21 |
Sets the day of the month
for a specified date according to local time. |
22 |
Sets the full year for a
specified date according to local time. |
23 |
Sets the hours for a
specified date according to local time. |
24 |
Sets the milliseconds for
a specified date according to local time. |
25 |
Sets the minutes for a
specified date according to local time. |
26 |
Sets the month for a
specified date according to local time. |
27 |
Sets the seconds for a
specified date according to local time. |
28 |
Sets the Date object to
the time represented by a number of milliseconds since January 1, 1970,
00:00:00 UTC. |
29 |
Sets the day of the month
for a specified date according to universal time. |
30 |
Sets the full year for a
specified date according to universal time. |
31 |
Sets the hour for a
specified date according to universal time. |
32 |
Sets the milliseconds for
a specified date according to universal time. |
33 |
Sets the minutes for a
specified date according to universal time. |
34 |
Sets the month for a
specified date according to universal time. |
35 |
Sets the seconds for a
specified date according to universal time. |
36 |
Deprecated - Sets the year for a
specified date according to local time. Use setFullYear instead. |
37 |
Returns the
"date" portion of the Date as a human-readable string. |
38 |
Deprecated - Converts a date to a
string, using the Internet GMT conventions. Use toUTCString instead. |
39 |
Returns the
"date" portion of the Date as a string, using the current locale's
conventions. |
40 |
Converts a date to a
string, using a format string. |
41 |
Converts a date to a
string, using the current locale's conventions. |
42 |
Returns the
"time" portion of the Date as a string, using the current locale's
conventions. |
43 |
Returns a string
representing the source for an equivalent Date object; you can use this value
to create a new object. |
44 |
Returns a string
representing the specified Date object. |
45 |
Returns the
"time" portion of the Date as a human-readable string. |
46 |
Converts a date to a
string, using the universal time convention. |
47 |
Returns the primitive
value of a Date object. |
Date Static Methods
In addition to the many
instance methods listed previously, the Date object also defines two static
methods. These methods are invoked through the Date( ) constructor itself −
Sr.No. |
Method &
Description |
1 |
Parses a string
representation of a date and time and returns the internal millisecond
representation of that date. |
2 |
Returns the millisecond
representation of the specified UTC date and time. |
Math Methods
Here is a list of each
method and its description.
Sr.No. |
Method & Description |
1 |
Returns the absolute
value of a number. |
2 |
Returns the arccosine (in
radians) of a number. |
3 |
Returns the arcsine (in
radians) of a number. |
4 |
Returns the arctangent
(in radians) of a number. |
5 |
Returns the arctangent of
the quotient of its arguments. |
6 |
Returns the smallest
integer greater than or equal to a number. |
7 |
Returns the cosine of a
number. |
8 |
Returns EN, where N is the
argument, and E is Euler's constant, the base of the natural logarithm. |
9 |
Returns the largest
integer less than or equal to a number. |
10 |
Returns the natural
logarithm (base E) of a number. |
11 |
Returns the largest of
zero or more numbers. |
12 |
Returns the smallest of
zero or more numbers. |
13 |
Returns base to the
exponent power, that is, base exponent. |
14 |
Returns a pseudo-random
number between 0 and 1. |
15 |
Returns the value of a
number rounded to the nearest integer. |
16 |
Returns the sine of a
number. |
17 |
Returns the square root
of a number. |
18 |
Returns the tangent of a
number. |
19 |
Returns the string
"Math". |
RegExp Methods
Here is a list of each
method and its description.
Sr.No. |
Method &
Description |
1 |
Executes a search for a
match in its string parameter. |
2 |
Tests for a match in its
string parameter. |
3 |
Returns an object literal
representing the specified object; you can use this value to create a new
object. |
4 |
Returns a string
representing the specified object. |
No comments:
Post a Comment
Please Do Not Enter Any Spam Link in the comment Box.