Javascript and Java

JavaScript and Java are similar in some ways but fundamentally different in some others. The JavaScript language resembles Java but does not have Java's static typing and strong type checking. JavaScript follows most Java expression syntax, naming conventions and basic control-flow constructs which was the reason why it was renamed from LiveScript to JavaScript.

Hello World

To get started with writing JavaScript, open the Scratchpad and write your first "Hello world" JavaScript code :

function greetMe (yourName) { 
    alert("Hello " + yourName); 
}
greetMe("World");

Select the code in the pad and hit Ctrl+R to watch it unfold in your browser!

Data Types

The latest ECMAScript standard defines seven data types :

Five data types that are primitives :

Declaring Variables

With the keyword var. For example,

var x = 42

This syntax can be used to declare both local and global variables.

By simply assigning it a value. For example,

x = 42
Variable Scope

JavaScript before ECMAScript 2015 does not have block statement scope; rather, a variable declared within a block is local to the function (or global scope) that the block resides within. For example the following code will log 5, because the scope of x is the function (or global context) within which x is declared, not the block, which in this case is an if statement.

if (true) {
    var x = 5; 
} 
console.log(x); // 5

This behavior changes, when using the let declaration introduced in ECMAScript 2015.

if (true) {
    let y = 5;
}
console.log(y); // y not defined