Javascript Concepts
Javascript is a language to create web applications. It is lightweight and easy to use language. Javascript helps HTML pages to be more than just a static page that does nothing. So, if you are planning to get good at this language, you should know some basic concepts.
Javascript concepts that you should know
- Scope of a variable
Scope determines the visibility of a variable in the code. There are two types of scope
1. Local Scope
2. Global Scope
Local Scope:- When a variable is defined inside a function then it is a locally scoped variable. The variable name that we use inside a function can only be accessed inside and can be renamed as same in any other function.
Global Scope:- When a variable is defined outside a function then it is a globally scoped variable. The name that is given to the global variable can only be used once and can not be declared again.
2. Hoisting
Hoisting is the process of javascript in which a declaration can be done before defining a variable. In simpler words, a variable can be used before it is declared
3. Strict Mode
Javascript has a special feature called “use strict” which makes the javascript code more efficient.
- use strict directive was new in ECMAScript version 5.
- use strict simply means that code should be written in strict mode. For example, we can not use an undeclared variable
- The expression “use strict” is written at the top of the document.
4. this keyword
The word “this” is a definition in itself. It points to the object it belongs to. It has different values according to where it is used in javascript
- In a method,
this
refers to the owner object. - Alone,
this
refers to the global object. - In a function,
this
refers to the global object. - In a function, in strict mode,
this
isundefined
. - In an event,
this
refers to the element that received the event. - Methods like
call()
, andapply()
can referthis
to any object.
5. Arrow Function
An arrow function(=>) is exactly what it looks like. It is used to shorten the function syntax and was introduced in ES6.
The this
the keyword has a bit different implementation in this case
In short, with arrow functions, there is no binding of this
.
In regular functions, the this
keyword represented the object that called the function, which could be the window, the document, a button or whatever.
With arrow functions, the this
keyword always represents the object that defined the arrow function.
6. Let and Const
There are different ways to create variables
Let:
Use let if you want to create a variable that really is variable.
Variables declared with the let
the keyword can have Block Scope.
Variables declared inside a block {} cannot be accessed from outside the block
Const:
Use const if you are creating value, so something which you assign once and never changes