Creating A Function In Javascript

The basic structure of a function looks like this:

function functionName() {
// the JavaScript you want to run
}

The keyword function lets the JavaScript interpreter know you’re creating a function—
it’s similar to how you use if to begin an if/else statement or var to create a variable.

function printToday()
{
var today = new Date();
document.write(today.toDateString());
}

The function’s name is printToday. It has just two lines of JavaScript code that retrieve
the current date, convert the date to a format we can understand
Related Posts:
  • Javascript cookie string We can set a cookie by setting document.cookie to a cookie string.  The following code will set a cookie with the UserName set as Paul,  and an expiration date of 28 December 2010. <html> <head> <… Read More
  • var keyword JavaScript The var and function are declaration statements—they declare or define variables and functions. These statements defineidentifiers (variable and function names) that can be used elsewherein your program and assign values to … Read More
  • Reserved Words in JavaScript Reserved Words in JavaScript 1.5 >abstract else instanceof switch >boolean enum int synchronized >break export interface this byte extends long throw case f… Read More
  • JavaScript Timers setTimeout() and setInterval() allow you to register a functionto be invoked once or repeatedly after a specified amountof time has elapsed. These are important global functions ofclient-side JavaScript, and are therefore de… Read More
  • JavaScript Security Downloading and running programs written by unknown parties is a dangerous proposition. A program available on the Web could work as advertised, but then again it could also install spyware, a backdoor into your system, … Read More
  • Expression Statements Assignment statements are one major category ofexpression statements.  For example: greeting = "Hello " + name;i *= 3; The increment and decrement operators, ++ and --, are relatedto assignment statements. These have … Read More
  • Include Inline JavaScript code To include inline JavaScript code, place JavaScript code inside the <script> element directly, as follows: <script type=”text/javascript”> function sayHi(){ alert(“Hi!”); } </script> When using inline Java… Read More
  • JavaScript Interview Questions JavaScript Interview Questions What is the difference between == and === ? The == checks for value equality, but === checks for both type and value. difference between innerHTML and append() in JavaScript? InnerHTML is… Read More
  • Creating Elements, Attributes, and Objects An important benefit of dynamic content is the ability to create new content from freshly received data. This is necessary in dynamic menu creation, navigation, breadcrumbs, and web services, among other applications. Ajax… Read More
  • concat() Method The concat() method returns the array resulting from appending its arguments to  the array on which it was invoked. Given the script: var myArray = ["red", "green", "blue"]; alert(myArray.concat("cyan", "yello… Read More
  • Retrieving the Response A complete HTTP response consists of a status code, a set ofresponse headers, and a response body. These are availablethrough properties and methods of the XMLHttpRequestobject:• The status and statusText properties return t… Read More
  • javascript Date, Time Best seo practices How to promoting webpages Top seo tips top 10 e-commerce tips Additional seo tips Advanced seo tips Mobile seo tips Javascript Date The methods getDate(), getDay(), getMonth(), and g… Read More
  • Prototypes Every Java-Script object has a second JavaScript object (or null, but thisis rare) associated with it. This second object is known as aprototype, and the first object inherits properties from theprototype.All objects created… Read More
  • HTTP “200 OK” response A complete request is not necessarily a successful request, andyour handler for the load event should check the status codeof the XMLHttpRequest object to ensure that you received anHTTP “200 OK” response rather than a “404 … Read More
  • WebSocket() constructor socket with the WebSocket() constructor:var s = new WebSocket("ws://ws.example.com/resource");The argument to the WebSocket() constructor is a URL that usesthe ws:// protocol (or wss:// for a secure connection like thatused … Read More