Here We Know about JavaScript from Beginning...
1. What is JavaScript?
=> JavaScript
is a Synchronous Single Threaded language
Single-threaded
=> JavaScript runs one command at a time.Synchronous
=> It Means JavaScripit runs command in Specific order, i.e. It runs next line when it finish current line.
2. How does JavaScript Work?
Everything in JavaScript happens inside Execution Context. -Execution Context contains two types of functionality:-
- I) Memory => Everything here we kept in key value pair, and it is also known as `variable environment.
such as:- a : 10;
- II) Code => In this box we write code that runs from up to down, it is also known as Thread of Execution.
3. What is Hoisting in JavaScript?
=> JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, before execution of the code.
- Hoisting allows the function to be safely used in code before they have been declared.
- In JavaScript variables can be used before they have been declared.
Example:-
The let
and const
keyword in Hoisting.
=> Variable declared with let
and const
are also hoisted, but they cannot be accessed before their declaration and initialized and this is also called the temporal dead zone.
this is happening because
var
is global scope butlet
andconst
is block scope, and they all are hoisted to the top of their scope.To avoid the Temporal dead zone always declare and initialize the variable at the top of the code.
4. How does function work in JavaScript?
- Function is one of the fundamental blocks in javascript.
- A Function is similar to a procedure - a set of statements that performs a task or calculates a value.
- function should take some input and return am output, where there is some relationship between the input and output.
=> For declare function, we have to follow some keywords:-
- The name of the function(we always have to remember that we should give a name of the function, otherwise we cannot invoke that function).
- A list of parameters to the function, is enclosed in parentheses and separated by commas.
- The JavaScript statements that define the function, enclosed in brackets,
{...}
.
function square(num){
return num*num
}
- Here input is
num
and output isnum*num
and the relationship between them is that num return square value of itself.
5. What is window
in JavaScript?
- The window object is supported by all the browsers, it represents the browser's window.
- All JavaScript objects, functions, variables automatically become members of the window object.
- Global variables are the properties of the window object.
- Global functions are the methods of the window object.
- Even the HTML document object(DOM) is the property of the window object.
6. What is the this
keyword in JavaScript?
- In JavaScript
this
keyword refers to the object it belongs to.
It has different values depending on where it is used
- In method
this
refers to the owner object.
const person = {
firstName: "John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
},
};
let fullname = person.fullName()
console.log('fullname:', fullname)
// output => fullname: John Doe
- Alone
this
refers to the global object and In function also refers to the global object.
let x = this;
// output => {} or [object Window]
- In function, in strict mode,
this
is undefined.
"use strict";
function myfunc() {
return this;
}
let result = myfunc();
console.log('result:', result)
// output => result: undefined
- Methods like
call()
andapply()
can referthis
to any object.
function Emp(id, name) {
this.id = id;
this.name = name;
}
function PermanentEmp(id, name) {
Emp.call(this, id, name);
}
function TemporaryEmp(id, name) {
Emp.call(this, id, name);
}
var p_emp = new PermanentEmp(101, "John Martin");
var t_emp = new TemporaryEmp(201, "Duke William");
console.log('p_emp:', p_emp)
console.log('t_emp:', t_emp)
/*
OUTPUT :-
p_emp: PermanentEmp { id: 101, name: 'John Martin' }
t_emp: TemporaryEmp { id: 201, name: 'Duke William' }
*/
7. Difference between undefined
and not defined
.
=> undefined
- It works like when we declare a variable in code but did not assign the value before printing the variable name.
- It is stored in the memory heap.
=> not defined
- It works like when we did not declare a variable but tried to call the variable name.
- It does not store in the memory heap.
- It gives a
Reference Error
.
8. What is the scope of JavaScript?
=> Scope means where you can access a specific variable or function in our code.
There is Two types of Scope in JavaScript
- Global Scope
- Local Scope
Global Scope
- When we start writing code in JavaScript documents, we are already in the global scope.
- The variable is in the Global scope if it is defined outside of a function.
var name = "Abhishek";
console.log(name) // output = Abhishek
function callName(){
console.log(name)
}
callName() // output = Abhishek
Here Name is in Global Scope
Local Scope
- Variable or function defined inside of a function are in Local Scope nad they have different scope for every call of that function.
- We can use the Same name of the variable in different functions.
function getName(){
var name = "Abhishek"
}
console.log(name) // it gives name is not defined
getName() // output = Abhishek
Here name is in Local Scope that's why when we try to console name outside the function it gives us `ReferenceError: name is not defined, and we try to call the function it returns the value of name.
9. What is Lexical Scope in JavaScript?
- Lexical Scope means that in nested function, the inner function has access to the variable and other resources of their parent function.
- That means the child function is lexically bound to the execution context of their parent function.
function parent() {
var role = "gurdian";
// name and like is not accessible here
function child() {
var name = "Alex";
// like is not accessible here
// role is accessible here
function grandchild() {
var like = 15;
// role and like is accessible here
}
}
}
10. What is Reference Error
in JavaScript?
=> The Reference error
object represents an error when the variable does not exist or haven't been initialized in the current scope is referenced.
11. What is a syntax error
in JavaScript?
=> A syntax error
occurs when a programmer writes an incorrect line of code.
- If the `syntax error occurs in the compiler, then the code does not work.
- Most
Syntax error
involves missing punctuation and misspelled name.
12. What is type error
or when does it occur?
- when an operand or argument passed to a function is incompatible with the expected by the operator or function.
- When trying to modify a value that can not be changed.
- when trying to use a value in an inappropriate way.
13. Difference between var
, let
and const
?
=> var
- It is the oldest keyword in javascript to declare a variable name.
- It is Global and functional scoped, it can access from anywhere. if we assign
var
variable in global scope we can access it inside the function, if we declare avar
variable inside the function then we can not access it in the global scope.
var a = 10;
function checkscope(){
console.log(a)
}
checkscope() // output = 10
function checkscope(){
var a = 12;
}
console.log(a) // output = ReferenceError: a is not defined
var
variable is re-declareable and updateable.
var a = 90;
console.log(a) // 90
var a = "Something"
console.log(a) // Something
a= 1;
console.log(a) // 1
=> let
- It is an improved version of the
var
variable. - The Scope of the
let
variable is Block Scoped. It can nor accessible outside a particular block.
let a = 10;
function f() {
let b = 9
console.log(b); // 9
console.log(a); // 10
}
f();
let a = 10;
function f() {
let a = 9
console.log(a); // 9
}
f();
console.log(a); // 10
- It can be updated but can not be re-declared.
let a = 8; a = 10; console.log(a); //10
let a = 8;
let a = 10;
console.log(a); // SyntaxError: Identifier 'a' has already been declared
-It can be declared without initialization.
=> const
- The Scope of the
const
variable is Block Scope. - It cannot be updated or re-declared into the scope.
- It cannot be declared without initialization.