function run(){
// Variable declaration.
var a = 1; // Create a number variable
var b = "hello"; // Create a string variable
var c = {}; // Create a object variable
var d = []; // Create a array variable
// Output the results
log( "Varible Test. " + a ); // Call a function with a parameter
log( "Varible Test. " + b ); // Construct the parameter at call time
log( "Varible Test. " + c );
log( "Varible Test. " + d );
// If block
var ifTest; // Declare a variable with no value
if( a === 1 ){ // Test for a specific value
ifTest = true; // Assign true or false as a boolean to a variable
} else {
ifTest = false;
}
log( "If Test. " + ifTest );
if( b.indexOf("bye") > 0 ){ // Test for a string inside a variable string
ifTest = true; // Assign true or false as a boolean to a variable
} else {
ifTest = false;
}
log( "If Test. " + ifTest );
// For loop
var forMessage = "";
for( var i=0; i<10; i++){ // Create a variable and loop through 10 iterations
forMessage = forMessage + i + ","; // Append a string and a number to a string
}
log( "For Test. " + forMessage );
// Create an Object with properties
var obj = { // Create a new object
forename: "Andy", // Add a forename property via declaration
surname: "Monis" // Add a surname property via declaration
}
obj.age = 36; // Add an age property via code
log( "Object Test. Name=" + obj.forename + " " + obj.surname + " Age=" + obj.age );
// Array Test
var arr = [ 'Andy' ]; // Create a new array
arr.push( 'Monis' ); // Add a new element to the array
arr.push( '180cm' );
// Join the array elements into a string for display
var str = arr.join( " " );
log( "Array Test. " + str );
}
Starting on WebApps - JavaScript Quick Start
JavaScript is a great language with a lot of rich functionality. However the greatest benefit of JavaScript is that you can get started with very little knowledge of the language. This example gives a brief overview of some of the language features and constructs to allow you to get started.
Subscribe to:
Posts (Atom)
No comments:
Post a Comment