Saturday, September 21, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Backend basics

JavaScript Backend basics

The popularity of Node.js has definitely boosted the use of javascript as a backend language, and in order to get started with javascript in the backend, you need to know some basics and general rules of this language. The following article is to get started with back-end development using JavaScript.

JavaScript Engine

Each browser has its own JavaScript engine which is used to support the JavaScript scripts in order for them to work properly. The basic job of a javascript engine is to take the javascript code, then convert it into a fast, optimized code that can be interpreted by a browser. Below are the names of the JavaScript engines used in some of the most popular browsers out there.

  • Chrome: V8
  • Firefox: SpiderMonkey
  • Safari: JavaScriptCore
  • Microsoft Edge/ Internet Explorer: Chakra/ChakraCore

ECMA Script StandardThe ECMA Script standard is a trademark scripting-language specification standardized by European Computer Manufacturers Association.ECMAScript provides the rules, details, and guidelines that a scripting language(like javascript)must observe to be considered ECMAScript compliant.

Types definitions in JavaScript

Dynamic Typing: The interpreter figures out the type of the variable dynamically based on certain conditions. Primitive Data Types: The primitive data types are the data types that have no methods attached to it i.e. some defined methods cannot be used with them and they are used in isolation. Though there are ways to use those methods by wrapping these primitive data type variables (covered in the next article). The following are the data types that come under the primitive category:

  1. undefined: If variable exists but is not defined then it is categorized under undefined.
  2. null: If variable exists but is not explicitly set then it comes under null category.
  3. boolean: Boolean represents a logical entity and can have two values: true, and false.
  4. number: The number is the data type to define a number which can be integer, floating-point, double. The only problem here is that we have to allocate a memory equivalent to a double variable every time we define a number.
  5. string: This is used to define string values of a character.
  6. symbol: This is a special data type which is new in ECMA Script 6. The data type “symbol” is a primitive data type having the quality that values of this type can be used to make object properties that are anonymous.

Object: Everything in JavaScript is an object. That is each variable, string, array or any other structure that we know comes under the category of object. Java Script object can be understood by almost every language and are easy to read. Creating objects: There are 4 ways to create objects: 1. Creating object with a constructor: 

javascript




<script>
    // Simple function
    function vehicle(name,maker,engine){
        this.name = name;
        this.maker = maker;
        this.engine = engine;
    }
    // New keyword to create an object
    let car  = new vehicle('GT','BMW','1998cc');
    // Property accessors
    console.log(car.name);
    console.log(car.maker);
    console.log(car['engine']);
</script>


Output: 2.Using Object literal 

javascript




<script>
    // creating js objects with object literal
    let car = {
        name : 'GT',
        maker : 'BMW',
        engine : '1998cc'
    };
    // property accessor
    console.log(car.name); // dot notation
    console.log(car['maker']); // bracket notation
</script>


Output: 3. Creating object with Object.create() method: 

javascript




<script>
    const coder = {
        isStudying : false,
        printIntroduction : function(){
            console.log(`My name is ${this.name}. Am I studying?: ${this.isStudying}`);
        }
    };
    const me = Object.create(coder);
    me.name = 'Mukul';
    me.isStudying = true;
    me.printIntroduction();
</script>


Output: 4. Using es6 classes: 

javascript




<script>
    // Using es6 classes
    class Vehicle {
      constructor(name, maker, engine) {
        this.name = name;
        this.maker =  maker;
        this.engine = engine;
      }
    }
     
    let car1 = new Vehicle('GT', 'BMW', '1998cc');
     
    console.log(car1.name);  // GT
</script>


Output: Coercion: What we call typecasting in C, C++, Java, it is called coercion in JavaScript.It is basically the process of converting a value from one type to another(like string to an integer, integer to boolean, etc). Coercion is of two types:

  •  Explicit Coercion Explicit coercion is the process by which we explicitly define a variable to a data type. 

javascript




let x = 42;
let explicit = String(x); // explicit is set to "42"
let explicit2 = x.toString();//another method to explicitly change type.


  • Implicit Coercion Implicit Coercion is the process by which the interpreter dynamically type casts the variable under certain conditions. 

javascript




let x = 42;
let implicit = x + " "; // interpreter automatically sets implicit as "42"


Scope

Variable lifetime: The variable lifetime is from where they are declared until their function ends. If no function is defined then scope of the variable is global. Hoisting: Function definitions are hoisted but not variable declarations. This means that when a function is declared, it is usable from anywhere within your code. For more information on hoisting in javascript visit this. The JavaScript engine works in two different phases:

  1. Creation Phase: Before executing the code, the engine reads through the entire file and will throw a syntactic error if one is found. While it does that, any function definitions will just be saved in memory. Any variable initialization will not be run but variable names will be declared.
  2. Execution Phase: The execution phase is the phase in which the code is run and hence the above variable hoisting example errors as undefined since, in the creation phase, the variable has been declared but not defined in the creation phase.

This article is contributed by Mukul Latiyan. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments