JavaScript in 15 minutes

Code | 15 August 2018

In this page, you will find the commonly used syntax and how to’s that are specific to JavaScript programming language. If you know what JavaScript (JS) is, but wanted to quickly refer to JS syntax, then you might find this page useful. I will keep on updating this article. So, kindly bookmark this page if you find it useful 😉

Console

For any type of JavaScript developer, console is the thing that makes them alive 😅. So, in this page, you will find console.log() mostly to display the output of a specific operation.

If you’re new to console in JavaScript, please open a browser (such as Google Chrome), right-click, inspect and click on console tab. From here, you can start executing the below code.

Variablescode

1
2
3
4
5
6
7
8
// define a variable
var name = prompt("What's your name?");

if(name != null) {
  console.log(name);
} else {
  alert("Please enter a valid name!");
}

Math Operationscode

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
// add
console.log("5 + 4 = ", 5 + 4); // prints 5 + 4 = 9

// sub
console.log("5 - 4 = ", 5 - 4); // prints 5 - 4 = 1

// mul
console.log("5 * 4 = ", 5 * 4); // prints 5 * 4 = 20

// div 
console.log("5 / 4 = ", 5 / 4); // prints 5 / 4 = 1.25

// mod
console.log("5 % 4 = ", 5 % 4); // prints 5 % 4 = 1

// max number
console.log(Number.MAX_VALUE); // prints 1.7976931348623157e+308

// min number
console.log(Number.MIN_VALUE); // prints 5e-324

// round off
a = 2.2555;
console.log(a.toFixed(2)); // prints 2.26

// pre increment
b = 1;
console.log(++b); // prints 2

// post increment
c = 1;
console.log(c++); // prints 1
console.log(c); // prints 2

// math functions
console.log(Math.abs(-8)); // prints 8
console.log(Math.cbrt(1000)); // prints 10
console.log(Math.ceil(6.45)); // prints 7
console.log(Math.floor(6.45)); // prints 6
console.log(Math.log(10)); // prints 2.302585092994046
console.log(Math.log10(10)); // prints 1
console.log(Math.max(10, 5)); // prints 10
console.log(Math.min(10, 5)); // prints 5
console.log(Math.pow(4, 2)); // prints 16
console.log(Math.sqrt(1000)); // prints 31.622776601683793
console.log(Math.floor(Math.random() * 10)) // prints 2

// conversions
a = "3.14"; // note this is a string
console.log(parseInt(a)); // prints 3
console.log(parseFloat(a)); // prints 3.14

Stringscode

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
var str = "This is a classic example for strings";

// length of string
console.log(str.length); // prints 37

// index of a substring
console.log(str.indexOf("example")); // prints 18

// slice a string based on start index and end index
console.log(str.slice(10, 17)); // prints classic

// slice a string based on start index
console.log(str.slice(10)); // prints classic example for strings

// substring using start index and length
console.log(str.substr(10, 7)); // prints classic

// substring replace
console.log(str.replace("classic", "awesome")) // prints This is a awesome example for strings

// get character at an index
console.log(str.charAt(10)); // prints c

// split string into an array 
console.log(str.split(" ")); // prints ["This", "is", "a", "classic", "example", "for", "strings"]

// upper case and lower case
console.log(str.toUpperCase()); // prints THIS IS A CLASSIC EXAMPLE FOR STRINGS
console.log(str.toLowerCase()); // prints this is a classic example for strings

Conditionalscode

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
// relational operators : === != > < >= <=
// logical operators    : && || !


// normal conditionals
var num = 2;
if (num < 4) {
  console.log("Pikachu!"); // prints Pikachu! 
} else if (num >= 4 && num < 10) {
  console.log("Charmander!");
} else {
  console.log("Pokemon!");
}

// switch statement
var age = 25;
switch(age) {
  case 21: 
      console.log("Awesome!");
  case 22: 
      console.log("Amazing!");
  case 23: 
      console.log("Astounding!");
  case 24: 
      console.log("Fantastic!");
  case 25: 
      console.log("Brilliant!");
  default:
      console.log("State of mind :)");
}
// prints Brilliant! State of mind :)

// ternary operator
var canVote = (age >= 18) ? true : false;
console.log(canVote); // prints true

Loopscode

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
//------------
// while loop
//------------
var i = 1;
while(i <= 5) {
  console.log("i = " + i);
  i += 1;
}
// prints 
// i = 1
// i = 2
// i = 3
// i = 4
// i = 5

//---------------
// do while loop
//---------------
var j = 5;
do {
  console.log("came here once!");
} while(i == 10);
// prints came here once!

//----------
// for loop
//----------
for(var k=0; k<4; k++) {
  console.log("k = " + k);
}
// prints
// k = 0
// k = 1
// k = 2
// k = 3

//-----------------------------------------------
// for loop - enumerable properties of an object
//-----------------------------------------------
var team = {captain: "dhoni", batsman: "kohli", bowler: "nehra"};
for(t in team) {
  console.log(t + " : " + team[t]);
}

// prints
// captain : dhoni
// batsman : kohli
// bowler  : nehra

Arrayscode

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
var arr = ["pikachu", "charmander", "bulbasaur", "squirtle"];

// accessing array items
console.log(arr[0]) // prints pikachu

// delete the 2nd item
console.log(arr.splice(2, 1)); // prints ["bulbasaur"]
console.log(arr); // prints ["pikachu", "charmander", "squirtle"]

// array to string
console.log(arr.toString()); // prints "pikachu,charmander,squirtle"

// array to string using join
console.log(arr.join(", ")) // prints "pikachu, charmander, squirtle"

// sort ascending
console.log(arr.sort()); // prints ["charmander", "pikachu", "squirtle"]

// sort descending
console.log(arr.reverse()); // prints ["squirtle", "pikachu", "charmander"]

// add item to the end
arr.push("mewtwo");
console.log(arr); // prints ["squirtle", "pikachu", "charmander", "mewtwo"]

// remove item from the end
arr.pop();
console.log(arr); // prints ["squirtle", "pikachu", "charmander"]

Objectscode

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
// object similar to dictionary in python
var team = {1: "gogul", 2: "deepan", 3: "mohan", 4: "sibi", 5: "sathiesh"}

// accessing keys and values
for(t in team) {
  console.log(t + " : " + team[t]);
}

// prints
// 1 : gogul
// 2 : deepan
// 3 : mohan
// 4 : sibi
// 5 : sathiesh

// get the keys in object
console.log(Object.keys(team)); 
// prints ["1", "2", "3", "4", "5"]

// get the values in object
console.log(Object.values(team)); 
// ["gogul", "deepan", "mohan", "sibi", "sathiesh"]

// accessing values using keys (this is not index)
console.log(team["2"]); // prints "deepan"

// object of object
var globalTeam = {main: "Not yet filled", sub: team};
console.log(globalTeam["main"]); // prints "Not yet filled"
console.log(globalTeam["sub"]);  // prints {1: "gogul", 2: "deepan", 3: "mohan", 4: "sibi", 5: "sathiesh"}

Functionscode

1
2
3
4
function multiply(a, b) {
  return (a*b);
}
console.log(multiply(5,4)); // prints 20

Try Catchcode

1
2
3
4
5
6
var a = 10; 
try {
  console.log("Hello World" + a + b);
} catch(err) {
  console.log(err.message); // prints "b is not defined"
}


In case if you found something useful to add to this article or you found a bug in the code or would like to improve some points mentioned, feel free to write it down in the comments. Hope you found something useful here.

Happy learning!