JavaScript Backend basics

The following article is to get started with back end development using JavaScript. This article would cover the basics and rules used in 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. Below are the names of the JavaScript engines used in some of the most popular browsers out there.

  • Chrome: V8
  • Firefox: SpiderMonkey
  • Safari: JavaScript Core
  • Microsoft Edge/ Internet Explorer: Chakra

ECMA Script StandardThe ECMA Script standard is a trademark scripting-language specification standardized by European Computer Manufactures Association. The ECMA Script standard sets the standard for JavaScript which is used throughout the web browsers available out there.

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 which 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 comes under the primitive category:

  1. undefined:- If variable exists but is not defined the it is categorized under undefined.
  2. null:- If variable exists but is not explicitly set the it comes under null category.
  3. boolean:- The Boolean data type is to define if a variable is True or False.
  4. number:- 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 3 ways to create objects:
1. Using the Object() Constructor

filter_none

edit
close

play_arrow

link
brightness_4
code

var obj = new Object();
obj.firstName = "Geeks";
obj.middleName = "for";
obj.lastName = "Geeks";
obj.isTeaching = "Javascipt";
obj.greet = function() { console.log("Hi"); }

chevron_right


2.Using Object literal

filter_none

edit
close

play_arrow

link
brightness_4
code

var obj = {} //obj is an empty object
obj.firstName = "Geeks";
obj.middleName = "for";
obj.lastName = "Geeks";
obj.isTeaching = "Javascipt";
obj.greet = function() { console.log("Hi"); }

chevron_right


3. Directly specifying the values

filter_none

edit
close

play_arrow

link
brightness_4
code

var obj = { 
    obj.firstName = "Geeks";
    obj.middleName = "for";
    obj.lastName = "Geeks";
    obj.isTeaching = "Javascipt";
    obj.greet = function() { console.log("Hi"); }
}

chevron_right


Coercion: What we call typecasting in C, C++, Java, it is called coercion in JavaScript.
Coercion is of two types:

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    var x = 42;
    var explicit = String(x); // explicit is set to "42"

    chevron_right

    
    

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

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.

Example of hoisting: –

filter_none

edit
close

play_arrow

link
brightness_4
code

function greet(){
    console.log("Hi");
}
greet();

chevron_right


The above function is executed successfully when called. But if we call the function first and then define it, then also it is successfully executed. This is called function hoisting.

filter_none

edit
close

play_arrow

link
brightness_4
code

greet();
function greet(){
    console.log("Hi");
}

chevron_right


However variable initialization are not hoisted.

filter_none

edit
close

play_arrow

link
brightness_4
code

x(); // calling x
var x = function(){
    console.log("Hi");
}
/* the above is an error as x is not a function because due to the '=' operator,
x is a variable initialization  to which a function is assigned.

chevron_right


The above example is the part which proves that variable hoisting is not possible in JavaScript.
But look at the next example which is another type of hoisting defined in JavaScript.

filter_none

edit
close

play_arrow

link
brightness_4
code

console.log(x);
var x = 42;
// Output ==> undefined

chevron_right


The above output errors as undefined and as we know that undefined type is when the variable exists but is not defined. So the question is how does the variable exists even if it is declared and initialized later?
This is another type of hoisting just like function hoisting i.e. the variable ‘x’ is available to us previously.

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 creation phase, the variable has been declared but not defined in the creation phase.

That’s it for this article. We’ll be covering some advanced topics in the next follow-up article.

This article is contributed by Anirudh Acharya. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.