JavaScript inline function | Basic code

  • by

An JavaScript inline function is a function assigned to a variable that is created at runtime instead of at parsetime. Consider an inline function to be a special case of a JavaScript function.

var func = function() { 
    //Code
};

Inline functions could also be declared with the more compact, arrow function syntax.

JavaScript inline function

Simple example code.

<!DOCTYPE html>
<html>
<body>

 <script>

  var foo = function (){ 
   alert('Inline function') 
 }
 setTimeout(foo, 100);

</script>

</body>
</html> 

Output:

JavaScript inline function

More Example

<script>
  
    var func = function() { 
      alert ('inline') 
    };
    $('a').click(func);

    // Alternative es6+ inline arrow function.
    let func2 = () => alert('inline');
    $('a').click(func2);

</script>

Inline JavaScript onclick function

You can use Self-Executing Anonymous Functions. this code will work:

<a href="#" onClick="(function(){
    alert('Hey i am calling');
    return false;
})();return false;">click here</a>

Or

You may inline any javascript inside the onclick as if you were assigning the method through javascript.Make code cleaner keeping your js inside a script block.

<a href="#" onclick="function hi(){alert('Hi!')};hi()">click</a>

Do comment if you have any doubts or suggestions on this JS function topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.