JavaScript Event

JavaScript Function

The definition expression defines a JavaScript function, and the value of such an expression is the newly defined function. In a sense, a function definition expression is a “function literal” in the same way that an object initializer is an “object literal.” A function definition expression typically consists of the keyword function followed by a comma-separated list of zero or more identifiers (the parameter names) in parentheses and a block of JavaScript code (the function body) in curly braces.

A lot of the values provided in the default environment have the type function. A function is a piece of program wrapped in a value. Such values can be applied in order to run the wrapped program.

JavaScript function

Functions contain blocks of code that need to be executed repeatedly. Functions can take zero or more arguments, and can optionally return a value.

Functions can be created in a variety of ways:

Function Declaration

function foo () { /* Technosap */ }

Named Function Expression

var foo = function () { /* Technosap */ }

I prefer the named function expression method of setting a function’s name, for some rather in-depth and technical reasons. You are likely to see both methods used in others’ JavaScript code.

Using JavaScript Function

A simple function

var greet = function ( person , greeting ) {
var text = greeting + ’, ’ + person ;
console . log ( text );
};
greet ( ’ Rebecca ’ , ’ Hello ’);

A function that returns a value

var greet = function ( person , greeting ) {
var text = greeting + ’, ’ + person ;
return text ;
};

console . log ( g r e e t ( ’ Rebecca ’ , ’ Technosap ’));

function that returns another function

var greet = function ( person , greeting ) {
var text = greeting + ’, ’ + person ;
return function () { console. log ( text ); };
};
var greeting = greet( ’ Rebecca ’ , ’ Technosap ’);
greeting ();

Self-Executing Anonymous Functions

A common pattern in JavaScript is the self-executing anonymous function. This pattern creates a function expression and then immediately executes the function. This pattern is extremely useful for cases where you want to avoid polluting the global namespace with your code — no variables declared inside of the function are visible outside of it.

A self-executing anonymous function

( function (){
var foo = ’ Hello world ’;
} ) ( ) ;
console . log ( foo ); // undefined !

Functions as Arguments

In JavaScript, functions are “first-class citizens” — they can be assigned to variables or passed to other functions as arguments. Passing functions as arguments is an extremely common idiom in jQuery.

Passing an anonymous function as an argument

var myFn = function ( fn ) {
var result = fn ();
console . log ( result );
};
myFn ( function () { return ’ hello world ’; }); // logs ’ hello world ’

Passing a named function as an argument


var myFn = function ( fn ) {
var result = fn ();
console. log ( result );
};
var my O the r F n = function () {
return ’ hello world ’;
};
myFn ( my O therFn ); // logs ’ hello world ’

A JavaScript function definition expression can also include a name for the function. Functions can also be defined using a function statement rather than a function expression.

Online Training Tutorials

  • JavaScript EventJavaScript EventJavaScript Event : Imagine an interface where the only way to find out whether a key on the keyboard is being pressed is to read the current state of that key. To be able to react to […]
  • JavaScript Node.jsJavaScript Node.jsJavaScript Node.js, a program that allows you to apply your JavaScript skills outside of the browser. With it, you can build anything from small command line tools to HTTP servers that […]
  • JavaScript ScopeJavaScript ScopeJavaScript Scope refers to the variables that are available to a piece of code at a given time. A lack of understanding of scope can lead to frustrating debugging experiences. When a […]
  • JavaScript “this” KeywordJavaScript “this” KeywordIn JavaScript "this" Keyword, as in most object-oriented programming languages, this is a special keyword that is used within methods to refer to the object on which a method is being […]
  • JavaScript User InputJavaScript User InputJavaScript User Input : Clicks are powerful and easy and you can add an on Click event to pretty much any HTML element, but sometimes you need to be able to ask for input from the user and […]
  • JavaScript BooleanJavaScript BooleanJavaScript Boolean, It is often useful to have a value that distinguishes between only two possibilities, like “yes” and “no” or “on” and “off”. For this purpose, JavaScript has Boolean […]
  • JavaScript Interview Questions and AnswersJavaScript Interview Questions and AnswersJavascript is an interpreted language with a C like syntax. While many people brush the language off as nothing more than a browser scripting language, it actually supports many advanced […]
  • JavaScript Comments with ExamplesJavaScript Comments with ExamplesJavascript supports two types of comments (JavaScript Comments). Double-slashes (//) tell javascript to ignore everything to the end of the line. You will see them used most often to […]