A quick introduction to the best high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language!
To start playing around with JavaScript, open the browser console (F12)!
// Single-line comment
/* Multi
Line
Comment */
Operation | Result |
---|---|
x + y |
Add both numbers |
x - y |
Subtract y from x |
x * y |
Multiply both numbers |
x / y |
Divide x by y |
x % y |
The remainder of dividing x by y |
// Declaring a variable
let identifier = 35;
var myVar = 20;
const myConst = 20; // Constant
let myLet = 20;
// Number
var age = 18;
// String
var lastName = "Smith";
// Boolean
var adult = true;
// Array
var types = [1, “1”, true];
// Object
var name = {first:"John", last:"Doe"};
Operation | Result |
---|---|
x < y |
Returns true if x is less than y |
x > y |
Returns true if x is greater than y |
x <= y |
Returns true if x is less than y |
x >= y |
Returns true if x is less than y |
Operation | Result |
---|---|
x == y |
Check if the values are equal |
x === y |
Check if value and type are equal |
x != y |
Check if the values are not equal |
x !== y |
Check if value and type are not equal |
"3" == 3 // true
"3" === 3 // false
Operation | Result |
---|---|
true |
Always true |
false |
Always false |
A && B |
True if both A and B are true |
```A | |
!A |
The opposite of A |
if (5 < 2) {
console.log ("Ummm...");
} else {
console.log ("Suprise!?");
}
if (5 < 2) {
console.log (“Ummm...”);
} else if (5 >= 2) {
console.log (“Suprise!?”);
} else {
console.log (“Whaaaat???”);
}
const arr = [23, 45, 78];
let i = 0;
while (i < arr.length) {
console.log (arr[i]);
i+= 1;
}
for (let i = 0; i < 10; i++) {
console.log (“index ” + i);
}
function add (num1, num2) {
return num1 + num2;
}
add (3, 4); // === 7
var str = "Hello World!";
str.length; // 12
str.indexOf(“!”) // 11
Math.floor(Math.random() * 10);
var object = {key: "value"};
object.key;
object["string key"];
const object = {key: "value"};
const array = ["abc", "xyz"];
const arrayObject = {0: "abc", 1: "xyz"};