JavaScript is a programming language that can be used to make websites interactive, it is ran entirely in the web browser (client side) after being downloaded from the server. JavaScript is interpreted, meaning it does not need to be compiled - it is written then ran.
JavaScript is the only language that can be used to script within the web browser - therefore it is the only choice when you're making a website with an interactive front-end component.
JavaScript follows the syntax of the Java language (however it's the only thing that it takes from Java), making it similar to C & C++ too. However, unlike these languages, data types are not declared for variables - instead they are inferred at runtime - meaning a variable can change type throughout a script (similar to Python's type system). In technical terms, it has a loose dynamic type system.
var helloWorld = "Hello World";
console.log(helloWorld);
var five = 5;
var six = 6;
if (five > six) {
console.log("Wrong!");
} else {
console.log("Right!");
}
Whilst most JavaScript interpreters are loose enough to allow scripts without semi-colons to be written, they should technically always be used at the end of statements (like in the example above). console.log() is simply a print to console statement - like println() in Java, or printf() in C.
JavaScript was created in 1995 by Brendan Eich, whilst working at Netscape. He made the language as a 'hack' project within 1 week (source). Since then, the language has been officially standardised by ECMA - making JavaScript an implementation of ECMAScript.
JavaScript includes all of the basic standard flow control statements you'd expect to find in a programming language - namely, if/else if/else statements for conditions, and for and while loops. Below is an example of them in action...
// This is a comment!
/* And this is a comment over
several lines */
// check sanity...
var isItTrue = true;
if (isItTrue) {
console.log("That was true!!");
} else if (isItTrue === 3) {
console.log("Wow such number 3");
} else {
console.log("Not true.");
}
// loop 5 times...
var count = 5;
for (var i = 0; i < count; i++) {
// print the number we're currently at in the loop...
console.log(count);
}
// to infinity and beyond
var condition = true;
while (condition) {
console.log("This is really not recommended");
}
In JavaScript, == checks if two variables have the same value (but they DO NOT have to be of the same type), === checks that they are of the same value AND the same type. This means "5" == 5 is true (because the Number representation of the "5" String is 5), but "5" === 5 is false, because they are of different types. There's more on Data Types in JavaScript next...