The next language to get the Hello Script treatment is JavaScript – my favorite language. Before we go any further, here is the definitions for Hello Script and JavaScript – just to make sure that we are on the same page…
- Hello Script
- ‘Hello Script’ is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet when working with that language.
- JavaScript
- JavaScript is a client side scripting language that is used in browsers.
Code
Warning: Do not run this code – you will get a lot of alerts. If you have firebug extension, uncomment the first line – then you can run the code.
<html>
<head>
<title>Hello Script for JS</title>
<script type="text/javascript">
//alert=console.log;
alert("Hello World");
// Variables, concatination
var name = 'Binny';
var year = 2008;
alert("Hello, " + name + " - welcome to " + year );
//If,else conditions
if (year > 2008) {
alert("Welcome to the future - yes, we have flying cars!");
}
else if(year < 2008) {
alert("The past - please don't change anything. Don't step on any butterflies. And for the sake of all thats good and holy, stay away from your parents!");
}
else {
alert("Anything wrong with your time machine? You have not gone anywhere, kiddo.");
}
// For loop
for(var i=0; i<3; i++) {
alert(i + ") Hi there!");
}
//Numerical Array, While
var rules = [
'Do no harm',
'Obey',
'Continue Living'
];
var i = 0;
while(i<rules.length) {
alert("Rule " + (i+1) + " : " + rules[i]);
i++;
}
// Associated array, foreach
var associated = {
'hello' : 'world',
'foo' : 'bar',
'lorem' : 'ipsum'
}
for(key in associated) {
alert(key + " : " + associated[key]);
}
// Using Join and Split
csv_values = "hello,world,how,are,you".split(",");
alert(csv_values.join(":"));
// Function, argument, return, call
function hello(name) {
return "Hello " + name;
}
hello_string = hello("Binny");
alert(hello_string);
// One for the OOP fanboys - Class, members, object and stuff.
function Movie(name) { //Constuctor
this.name = name;
}
Movie.prototype.rateMovie = function() {
this.rating = (this.name.length % 10) + 1; //IMDBs rating algorithm. True story!
}
Movie.prototype.printMovieDetails = function() {
alert("Movie : " + this.name + "\nRating : " + this.rating);
}
//Create the object
ncfom = new Movie("New Country for Old Men"); //It's a sequel!
ncfom.printMovieDetails();
// Regular Expressions
string = "Hello World";
if(string.match(/^Hell/)) alert("Yup - its evil\n");
alert(string.replace(/l([^l])/g, "$1")); //Remove an 'l' from both words. Should alert('Helo Word'
/**
* Specialized code
*/
window.onload = function() {
var ele = document.getElementById("div-element");
alert(ele);
ele.innerHTML = "Hello World";
ele.onclick = function() {
alert("You Clicked?");
}
document.getElementById("text").value = "Goodbye World";
}
</script>
</head>
<body>
<div id="div-element"></div>
<input type="text" value="3" id="text" />
</body>
</html>
Good work again Binny.
Thanks for that.
Any suggestion on which language I should tackle next?
Java,
C#,
VB,
there are lots…