Javascript is a programming language which can interact easily with browsers. Most of the web browsers support javascript by default. In starting days javascript acts with only HTML pages to make it interactive. But nowadays, javascript can run everywhere from web to mobile.
We can run javascript in the browser frontend natively and using node js environment we can run javascript in server too. Node js basically provides an environment to execute javascript without a browser in the server.
Let’s start with a simple “hello world” code.
console.log(“hello world”) |

In javascript we have many events, from them we have “onclick” events.
We can easily incorporate javascript code with HTML pages.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <p id="p1">this is a paragrah</p> <button onclick="func1()">click</button>
<script> function func1(){ console.log(document.getElementById("p1").innerHTML) } </script> </body> </html> |

We can set any HTML content on the button onlick event.
<p id="p2">i love marvel</p>
<script> function func1(){ console.log(document.getElementById("p1").innerHTML) document.getElementById("p2").innerHTML = "i love dc" } </script> |
We have 3 types of console messages in js.
<script> console.log("this is simple log"); console.warn("this is warning") console.error("it shows an error message") </script> |

We can declare variables in js in 3 ways - let, var & const.
let a=10; var b=20; const c=70;
// we can decalre a variable once using let keyword // let a="abacus";
// we can redeclare and reassign value to a variable var b="canon"; console.log(b); b=536.67; console.log(b);
// we can't reassign value to a constant variable c=67; |
In js we have 2 types of comments - single line and multiline.
// this is a single line comment
/* this is a multiline comment */ |
We can perform basic arithmetic operations in js.
let a,b; let sum, minus, pro, div, quo; a=10;b=31;
sum=a+b; console.log(sum);
minus=b-a; console.warn(minus);
pro=a*b; console.log(pro);
div=b/a; console.warn(div);
quo=b%a; console.log(quo); |
We can apply logical operators in js.
let a=10; let b=20;
// logical and - && console.log(a==10 && b==20); // t console.log(a==10 && b==21); // f
// logical or console.warn(a==10 || b==21); // t console.warn(a==11 || b==22); // f
// not console.log(a!=23); // t console.log(!b==20); // f |
Javascript supports various data types. We can know a variable data type using typeof() method.
let a;
a='abacus'; console.log(typeof(a));
a=23; console.log(typeof(a));
a=23.5 console.log(typeof(a));
a=true; console.log(typeof(a));
a=null; console.log(typeof(a)); |
In javascript we declare our own functions and we can also call them.
function ab(a, b) { console.log("hello", a, b); }
ab('abacus', 20) |
We can pass parameters into it.
We can access variables that are declared.
let name = "dom"; function ab(a, b) { console.error(name); console.log("hello", a, b); }
ab('abacus', 20) console.warn(name); |
We can make objects inside javascript. Objects are like variables, but they can store more values compared to variables. It stores value in the form of key and value pairs. These key and value pairs are also known as ‘properties’.
a = {"name":"dom","car":"mustang"}; console.log(typeof(a)); // object console.log(a.name,a.car); // dom, mustang
const per = { "name": "dom", "phone": { "ph1": 44545, "ph2": 58506 }, car: function () { let name = "musang"; return name } }; console.log(per.phone.ph1); // 44.. console.warn(per.car()); // mustang |
We can access key and value pairs.
const per2 = {"name":"dom","phone":987856,"address":"kol"};
// accesing object keys and values for(a in per2){ console.log(a,per2[a]); } |
We can make arrays inside js for various work.
let x; let a = [10,20,'hallo']
console.log(a); console.log(typeof(a)); // array
// iterating each element through for loop for(x=0;x<3;x++){ console.log(a[x]); }
// we can use while loop too. x=0; while(x<3){ console.log("element is: ",a[x]); x=x+1; }
let b; b=['dom','mia','brian',90,-78]; // we can sort array's elements console.log(b.sort()); |
We can handle exceptions in the js using try-catch blocks. Also remember the finally black always executes whenever the try or catch block executes.
let a;
try{ a=10/0; console.log(a); } catch(err){ console.log(err); } finally{ console.log("this block alwayes run"); } |
We can make a custom exception message using the ‘throw’ keyword.
let a;
try{ if(a=10/0){ throw "custom error" } } catch(err){ console.log(err); } |