Getting setup
I always find the best way to get started with a language is to get some sort of REPL installed, for Javascript this is node.js. This provides a CLI that you can try many of the concepts of the language without having to learn the intracies of an IDE and its environment. For more details about node.js head over to https://nodejs.org/en/download/
Language Basics
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | // Single line Comment /* Multi line Comments */ let x = 2; // Variable const y = 3; // Constant let x = 1, y = 2, z = 3; // Multi variable assigment let x = 1, y = 2, z = 3; // Multi variable assigment // Numbers > "Fred" / 3 NaN > 1 / 0 Infinity // Infinity > -1 / 0 -Infinity // Negative Infinity let x = 2 // Int // the "n" at the end means it's a BigInt const bigInt = 1234567890123456789012345678901234567890n; // Strings let str1 = "Keith". // Double Quotes let str2 = 'Sterling'. // Single Quptes let str3 = str1 + str2 // Use of the back tick ` for strong substitution let str4 = `Hello ${str1} ${str2}` // Booleans true // True false // False true && true // True true && false. // False false && false // False true || true // True true || false // True true || false // True !true // False !false // True // null let age = null // age = null > age null // undefined let years // years = undefined > years undefined // typeof typeof undefined // "undefined" typeof 0 // "number" typeof 10n // "bigint" typeof true // "boolean" typeof "foo" // "string" typeof Symbol("id") // "symbol" typeof Math // "object" typeof null // "object" typeof alert // "function" typeof Infinitiy // "number" typeof -Infinity // "number" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | // String conversions let bVal = true; let sVal = String(bVal); > sVal 'true' let iVal = 32 let sVal2 = String(32) > sVal2 '32' let fVal = 45.8889 let sVal3 = String(fVal) > sVal3 '45.8889' // Number Conversions let nVal1 = "3" + "2" // nVal1 = "32" let nVal2 = "10" / "2" // nVal2 = 5 let nVal3 = "10" * 3 // nVal3 = 30 let nVal4 = "100" - "10" // nVal4 = 90 let nVal5 = Number("678") // nVal5 = 678 let nVal6 = Number("Keith") // nVal6 = Nan let nVal7; // nVal7 = undefined let nVal8 = Number(nVal7) // nVal8 = NaN let nVal9 = Number(true) // nVal9 = 1 let nVal10 = Number(false) // nVal10 = 0 let nVal11 = Number("") // nVal11 = 0 let nVal12 = Number(" 1 ") // nVal12 = 1 let nVal13 = Number("\t6") // nVal13 = 6 let nVal14 = Number(" 7 7 ") // nVal14 = NaN // Number short hand let nVal15 = +"2" // nVal14 = 2 +true // 1 +"" // 0 // Boolean Conversions Boolean(1) // true Boolean(0) // false Boolean(99) // true Boolean(3.4) // true Boolean(0.9) // true Boolean(-1) // true Boolean("Hello") // true Boolean("0") // true !! Boolean("") // false Boolean(null) // false Boolean(undefined) // false Boolean(NaN) // false |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // Basic Operators let x = 1; let y = 1; x = -x; // Unary Operation let x = x + y // Binary Operation x + y; // Addition x - y; // Subtraction x * y; // Multiplication x / y; // Division x % y; // Remainder x ** y; // Exponentiation x to the power of y "1" + 2; // "12" 1 + "2" // "12" 2 + 2 + '1' // '42" '1' + 2 + 2. // "122" 6 - '2' // 4 '6' / '2' // 3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | let x = 2 // x = 2 x = 3 // x = 3 let a, b, c // a = b = c = undefined a = b = c = 2 // a = 2, b = 2, c = 2 c = 3 - (a = b + 1); // b = 2, a = 3, c = 0 let x = 1; // x = 1 x += 2; // x = 3 x *= 3; // x = 9 x /= 3; // x = 3 x -= 2; // x = 1 x = 2; // x = 2 let y; // y = undefined y = x++; // y = 2, x = 3 y = ++x; // y = 4, x = 4 y = --x; // y = 3, x = 3 y = x--; // y = 3, x = 2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // Bitwise Operators // AND ( & ) // Returns a one in each bit position for which the corresponding bits of both operands are ones. 15 & 9 // = 9, 1111 & 1001 = 1001 // OR ( | ) // Returns a zero in each bit position for which the corresponding bits of both operands are zeros. 15 | 9 // = 15, 1111 | 1001 = 1111 // XOR ( ^ ) // Returns a zero in each bit position for which the corresponding bits are the same. [Returns a one in each bit position for which the corresponding bits are different.] 15 ^ 9 // 6, 1111 ^ 1001 = 0110 // NOT ( ~ ) // Inverts the bits of its operand. ~15 // -16, ~ 0000 0000 … 0000 1111 = 1111 1111 … 1111 0000 // LEFT SHIFT ( << ) // Shifts a in binary representation b bits to the left, shifting in zeros from the right. 9<<2 // = 36, because 1001 shifted 2 bits to the left becomes 100100, which is 36. // RIGHT SHIFT ( >> ) // Shifts a in binary representation b bits to the right, discarding bits shifted off. 9>>2 // = 22, because 1001 shifted 2 bits to the right becomes 10, which is 2. Likewise, -9>>2 yields -3, because the sign is preserved. // ZERO-FILL RIGHT SHIFT ( >>> ) // Shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left. 19>>>2 // = 4, because 10011 shifted 2 bits to the right becomes 100, which is 4. For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | // Comparisons // Numbers let a = 1; let b = 2; a > b // false a < b // true a == b // false a != b // true // Strings 'Z' > 'A' // true 'Man' > 'Woman' // false // Different Types '2' > 1 // true, '2' converted to 2 '01' == 1 // true, string '01' becomes a number 1 // Careful now let a = 0; Boolean(a); // false, 0 is boolean false let b = "0"; Boolean(b); // true, a non empty string is true a == b; // true!, b converted to numeric 0 alert( 0 == false ); // true, both numeric 0 alert( '' == false ); // true, both converted to numeric 0 alert( 0 === false ); // false, different types alert( null > 0 ); // false, > differnet to == alert( null == 0 ); // false, < differnet to == alert( null >= 0 ); // true, == converts null to 0, and comparison is true alert( undefined > 0 ); // false, undefined converted to Nan, special comparison, always false alert( undefined < 0 ); // false, undefined converted to Nan, special comparison, always false alert( undefined == 0 ); // false, undefined converted to Nan, special comparison, always false |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | // Branching if (year == 2015) do_something() // If block if (year == 2015) { do_something(); } // If else if (year == 2023) { do_this(); } else { do _that(); } // If then if then else if (year > 2022 { do_this(); } else if ( yeah > 2020 && year < 2023 ) { do_that(); } else { do_the_other () } // ? Operator as alternative if () let x = year == 2023 ? do_this() : do_that (); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | // Logical Operators let a = 1; let b = 2; // OR let result = a || b; // 1 // AND result = a && b; // 2 // NOT result = !b; // false // Convert to Boolean result = !!"non-empty string" // true // ?? Coalescing operator a ?? b // a if is defined, else b let user; let name = user ?? "Anonymous; // name = 'Anonymous' let user2 = "keith; let name2 = user ?? "Anonymous; // name = 'keith' firstName ?? lastName ?? nickName ?? "Anonymous" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | do { // loop body } while (condition); for (begin; condition; step) { // ... loop body ... } for (;;) { // repeats without limits } while (condition) { if (some_conditioN()) { break; } } outer: while (condition) { while (condition) { if (some_condition2()) { break outer; } } } while (condition) { if (some_conditioN()) { continue; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | switch(x) { case 'value1': ... [break] case 'value2': case 'value3': ... [break] default: ... [break] } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | // Functions function name() { // body } function name() { // body return value } function name(parameter1, parameter2, ... parameterN) { // body } function name(parameter1, parameter2, ... parameterN) { // body return value } function name(parameter1, parameter2 = "Default Value") { // body } // Scope let userName = 'John'; function showMessage() { let userName = "Bob"; // declare a local variable let message = 'Hello, ' + userName; // Bob alert(message); } // the function will create and use its own userName showMessage(); alert( userName ); // John, unchanged, the function did not access the outer variable // Arrow Functions let func = (arg1, arg2, ..., argN) => expression; let sum = (a, b) => { // the curly brace opens a multiline function let result = a + b; return result; // if we use curly braces, then we need an explicit "return" }; |