Learn Javascript With Me

JavaScript Basics

1. Variable Declaration And Initialization

Declaration and Intialization are two different things, if not done in a proper way it shows some exceptions. While declaring and initializing, Javascript hoisting may create some trouble if we do not properly use it.
Lets be clear from a simple example:

In the above example we declared and initialized the variable 'x' after it is being used. Here Javascript hoisting comes into play and moves its declaration at the top but the initiaalization remains where it is. So when we run this script it displays undefined.



Now to solve this problem what we can do is, either we can declare and initialize the variable 'x' before using it or we can initialize it before using and declare it after using it. Here What javascript hoisting will do is, it will simply move the declaration part to the top and thats it, code runs fine.



Output will be:

2. Errors that you may encounter if you do not properly declare and assign a variable

Basically you may face two kinds of errors:
i. Reference Error and
ii. Undefined
   Reference error arises if you have not declared a varibale but you are trying to use it.
This example gives you reference error:



Error:



Undefined is the case when you try to use a variable that you have declared but have not assigned any value to it.
This example gives you undefiend result:



Output:

3. Data Types in Javascript

To work with variables, it is necessary to know about the type of a variable.
Basically JS has 8 basic data types.

i. number :- variable with number data type stores integer or floating-point values.
ii. bigint :- integer numbers(of arbitrary length).
iii. string :- for string values.
iv. boolean :- true/false.
v. null :- unknown values.
vi. undefined :- unassigned value.
vii. object :- complex data structures.
viii. symbol :- unique identifiers.

Among these there are three primitive data types:

i. string
ii. number
iii. boolean

In javascript we do not explicitly declare any variable with any data types. During runtime data type is defined for a variable as per the value it poses.
Lets be clear through an example:



In this example 'typeOf' operator is used, which returns the actual data type of a variabe as per the value it holds.
Output for this example is:

4. JavaScript is a prototypical language

JavaScript is a functional programming language not a class based language. Prototypical language suggests that it is a style of OOP in which behaviour reuse or simply inheritance is achieved by reusing existing objects that serve as a prototypes. It uses generalized objects, that can be extended and cloned.
Methods and properties are inherited by all the objects from a prototype. Go through example to understand better:



In this example there is a function Student which has its properties as name,standard,age, and grades. Here Student acts as a prototype and when we create new objects student1 and student2 referencing Student, these two objects student1 and student2 inherit the properties of Student. And student1 and student2 can now access the properties of Student as you can see in the example at line number 25 and 26.

Output:

5. Creating objects in JavsScript

In JavaScript there are different ways for creating objects:

i. using object literals
ii. using new Keyword or constructor.

When we create an object using object literals, we define and create an object in just a single statement.

We create objects using object literals as:

var Student={fullname:"Hari Bahadur",age:"15",standard:"10"};

Using new keyword or constructor we create objects as:

var student1=new Student();
student1.name="Hari Bahadur";
student1.age="15";
student1.standard="10";



Output: