MyBlogHelp– A Blog for Digital Marketing, Affiliate Marketing, Organic Lead Generation.
  • Home
  • About
  • Contact
  • Privacy
  • Sitemap
  • BLOG
  • AFFILIATE MARKETING
    • LeadsArk
  • MORE
    • MAKE MONEY
      • ONLINE BUSINESS
      • ADSENSE
      • AFFILIATE MARKETING
    • BLOGGING
      • SEO
      • BlogSpot
      • WORPRESS
      • GOOGLE
      • COMPUTER TIPS
    • WEB DESIGN
      • HTML
      • CSS
      • BOOTSTRAP
      • JAVASCRIPT
      • JQUERY
    • WEB DESIGN
      • HTML
      • CSS
      • BOOTSTRAP
      • JAVASCRIPT
      • JQUERY
    • WEB DEVELOPMENT
      • PHP
      • WORDPRESS
  • DOWNLOADS
    • Blogger Template
    • Wordpress Theme
    • PDF

Thursday, November 28, 2019

How to use functions in JavaScript?

 MyBlogHelp     November 28, 2019     javascript     3 comments   

JavaScript Functions

 

   JavaScript - Functions

  1. Introduction to JavaScript functions
  2. JavaScript built-in functions
  3. Defining JavaScript functions
  4. Call JavaScript functions


Introduction to JavaScript Functions

By using functions in JavaScript you can use any code repeatedly in many places in the program and you can avoid the problem of writing the same code many times.

This increases the reusability of the code, that is, you can apply the same code to many places in the program as per the requirement. Functions make your program more structured and readable.

JavaScript provides some built-in functions that you can use in your program. These functions perform some regular basic tasks.  Let's try to know about these functions.

JavaScript Built-in Functions

There are five built-in functions in JavaScript that you can access and use according to your requirements.

eval ()
This function evaluates the passed string. If a string is a number then returns it, otherwise, it gets terminated.

parseInt ()
In this function, two parameters are passed. The first parameter is a string and the second parameter is an integer that represents the base such as 10 or 16. It returns a number of that base according to the function string.

parseFloat ()
This function reads a string and if a string is a float then this function returns it.

escape ()
This function returns a string by converting it into another character. Such as at the place of space it returns + sign and at the place of alphanumeric values, it returns %.

unescape ()
By this function the result of the escape () function can be converted back into the original.

Defining JavaScript Functions

It's easy to define functions in JavaScript. For this, you use a function keyword. After this keyword, you type the name of the function and then define the list of parameters.

The structure of the function is given below.

Syntax

function functionName(parameters_ list)
{
    // your code will be here.
}

You can create any JavaScript function on the basis of this structure. An example of creating functions in JavaScript is being given below.

<script type="text/javascript"> 
function hello()
{
   document.write("Hello, how are you?");
} 
</script>

Call JavaScript Function

After defining the function, you have to call it to use. You can call Function anytime in the entire script. For this, you write the name of the function thereafter putting the brackets and end with a semicolon. An example is being given below.

<! - JavaScriptFunctionsExample.html ->

<!DOCTYPE html>
<html>
<head>
<title> javascript function calling example </title> 
<script type="text/javascript">
// Defining function
function hello()
{
    document.write("Hello! How are you?");
}
</script>
</head>
<body> 
<script type="text/javascript">
// Calling function
hello();
</script>
</body>
</html>

The above script will generate the below output.
Hello! How are you?

JavaScript Function Parameters

If you want, you can pass a value to the function and the function can evaluate this value and show the output. You define the parameter in brackets to pass a value to the function. To define the parameter, you name the variable that will hold the value. An example is being given below.

<script type="text/javascript"> 
function hello (name) //Here name is a parameter
{
   document.write("Hello" + name);
}
</script>

After defining the parameter, when you call the function, you pass a value for that parameter. By doing so, it gets the value function and the function execution on it. An example is given below.

<! - JavaScriptParameterFunctionExample.html -> 

<!DOCTYPE html>
<html>
<head> <title> JavaScript Parameter Example </title> <script type="text/javascript">// function greeting(name) { document.write("Hello friend! How are you? " + name); } </script> </head> <body> <script type="text/javascript"> // Calling greeting('Abdussamad'); </script> 
</body>
</html>

The above script will generate the below output.
Hello friend! How are you? Abdussamad


         <<-- PREVIOUS                                                             NEXT -->>






  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

3 comments:

  1. Anjali VinuFebruary 1, 2020 at 11:15 AM

    Excellent post, thanks for this. I gathered lots of information from this and I am happy about it. Do share more updates.
    PHP Training in Chennai
    Best PHP Training Institute in Chennai
    PHP Institute in Chennai
    Angular Training in Chennai
    Web Designing Training in Chennai
    Salesforce Training in Chennai
    Tally course in Chennai
    Machine Learning course in Chennai
    PHP Training in Velachery
    PHP and MYSQL Interview Questions

    ReplyDelete
    Replies
      Reply
  2. averageFebruary 5, 2020 at 10:04 PM

    I never used JavaScript. It may sound very ridiculous, but, I always thought that Java is a game platform. I was so wrong about it.

    ReplyDelete
    Replies
      Reply
  3. Banu RaviMarch 24, 2020 at 5:07 PM

    Excellent post, it will be definitely helpful for many people. Keep posting more like this.
    Salesforce Training in Chennai
    Salesforce Course in Chennai
    Salesforce Training
    AngularJS Training in Chennai
    ccna course in Chennai
    React JS Training in Chennai
    Tally course in Chennai
    Tips for Salesforce Professionals
    Salesforce Training in Velachery

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

Thank you for reading this post. Please do not enter any spam link in the comment box.

Followers

Blog Archive

  • ►  2020 (4)
    • ►  October (1)
    • ►  September (1)
    • ►  August (2)
  • ▼  2019 (52)
    • ►  December (1)
    • ▼  November (2)
      • How to use functions in JavaScript?
      • How to use strings in JavaScript?
    • ►  October (1)
    • ►  August (1)
    • ►  July (1)
    • ►  May (11)
    • ►  April (10)
    • ►  March (6)
    • ►  February (3)
    • ►  January (16)
  • ►  2018 (54)
    • ►  December (27)
    • ►  November (5)
    • ►  May (5)
    • ►  April (4)
    • ►  February (3)
    • ►  January (10)
  • ►  2017 (17)
    • ►  December (9)
    • ►  November (8)

Popular Posts

  • How to use the main tag in html5?
  • How to use keygen tag in html5?
  • How to use html5 mark tag to highlight text?
  • How to start Online Business without money from home?
  • What is a search engine?

Contact Us

Name

Email *

Message *

About MyBlogHelp

Most of the posts, we share on this blog related to Affiliate Marketing, Organic Lead Generation, Digital Marketing, Social Media Marketing, Make Money Online, SEO, and web design.

Featured Post

LeadArk - Affiliate Marketing | Qualified Organic Lead Generation | LeadsArk Review

Copyright © MyBlogHelp– A Blog for Digital Marketing, Affiliate Marketing, Organic Lead Generation.
Design by Md. Abdussamad | MyBlogHelp.com