JavaScript -Fundamentals

What is JavaScript

Java Script is a Programming language that is used in web development. it responsible for dynamic content of a web page. JavaScript is supposed to run in a web browser, but it is possible to run outside the browser using node js runtime environment. So it means Java Script can use for both frontend and backend development.

Features of Java Script

JavaScript is a loosely typed programming language. it means you don’t need to specify what kind of data you are going to store in variables.

JavaScript variables

JavaScript variables can define using several keywords. Each keyword has its own scope.

JavaScript ‘this’ keyword

How this Keyword behave in Java Script?

Ex: person = { firstName: "hello", lastName: "world", getFullname: function(){return this.firstName + this.lastName}};
Ex: var x  =this;
Ex: function  myFunction(){    return this;}
Ex: "use strict";function  myFunction(){    return this;}
Ex: <button onClick="this.style.display = 'none'"></button>

JavaScript asynchronous behavior

Like mentioned earlier, JavaScript run on a single thread and it behave asynchronously.

Ex: function myFunction(){    let value = 0;    setTimeout(function(){        value = 10;         }, 2000);    console.log(value);}myFunction();

JavaScript Callbacks

In JavaScript, you can pass a function as an argument to another function. Those functions are called callback functions.

EX: function myFunction(callBack){        callBack();}function myCallback(){    alert("I am the callback");}myFunction(myCallback);
Ex: function myFunction(callBack){

setTimeout(callBack(), 1000);
}

function displayMessage(){

alert("I will display after one second!");

}

myFunction(displayMessage);

JavaScript Promises

The promise is an object that returns a single result by asynchronous operations in JavaScript. It represents the failure or success of the operation. JavaScript promise contains,

Ex: function checkEvennumber(number) {    let remainder = number % 2;    return new Promise(function (resolve, reject) {        if (remainder === 0) {            resolve("OK");        } else {            reject("No");        }    });}checkEvennumber(20).then(    function (message){alert(message);},    function (error){alert(error);}    );

async functions

async functions are supposed to return a promise.

await

The await keyword only used inside an async function.

function getValue(){
return new Promise(function (resolve,error) {
setTimeout(function () {
let value = 10;
resolve(value);
}, 2000);
});
}

function setValue(value){
return value + 50;
}

function getNewValue(value){

return value *100;
}

async function print(){
let value = await getValue();
value = await getNewValue(value);
value = setValue(value);
return value;
}


print().then((function (value){
console.log(value);
}));

Arrow functions

Arrow functions are used to write functions in shorter syntax.

Ex: hello = function myfunction(){    return "hello";}

Arrow function with parameter

welcome = (name,module) => “Hi “ + name +” welcome to “ + module;

Undergraduate Software Engineer at Sri Lanka Institute of Information Technology