Working process of JavaScript- a brief discussion about JS
JavaScript is a single-threaded, client-side scripting language, and synchronous in nature. JS is an interpreted language, which means it does not need a compiler to translate its code whereas C or C++ is needed. JS code runs directly in a web browser.
Every browser provides a JavaScript engine nowadays that runs the JavaScript code. Google Chrome uses the V8 engine which is open source, developed by Google, written in C++ to run JavaScript. The JS source code goes through many steps and finally gets executed. When the browser loads the page, the browser has a built-in interpreter that reads the JavaScript code then finds it on the page, and runs it.
When we run a JavaScript program inside a web browser, JavaScript code is received by the browser's engine in google chrome which is the V8 and this V8 engine runs the source code to get the output. All steps-
Parser- JS code first received by the parser and parser job is to check syntactic errors line by line if get then code executions is stopped. Once the parser checked the code and is sure that there is no error in the code then it makes a data structure which is called AST. Once the AST which stands Abstract Syntax Tree is created by the parser, the JavaScript engine converts the JavaScript code into the machine understanding code. When the code gets converted into the machine code or byte code, the converted code is sent to the system for execution, and finally, that byte code is run by the system/ V8 engine which uses JIT compilation for improved performance.
JavaScript code execution in Browser
JS isn’t a compiled language, it’s an interpreted language. So, for the code execution, there is no need to compile before the code run.
The JavaScript source code first passed through a program called a compiler, which translates it into bytecode or machine code that the machine could understand and could execute. Thus, JavaScript has no compilation step that’s why the browser reads the code, and code is interpreted line by line using JIT which compiles JavaScript code to executable bytecode just in time to run and run it inside the browser.
Differences between “==” and “===”?
Answer: Basically, ‘’==’’ and ‘’===’’ both are comparison operators. The main difference between these two operators is that “= =” is used to compare only values not data type whereas, “=== “is used to compare both value and data types.
**
What are the differences between var, let, and const?
**
There are three ways to declare variables in JavaScript. They are var, let, and const. The difference between these is given below-
Variables declared with var are in function scope but let and const are in the block scope.
Hoisting means we can define a variable before the declaration that is allowed in var but let & const not allowed hoisting.
Reassign value is permitted to var & let but const doesn’t permit to reassign value.
Redeclaration is allowed in var but let & const don’t allow redeclaration.
The variables declared with the var and let keyword are mutable (means value can be changed), but const is immutable.
**
Why will we use default parameters?
**
In JavaScript, Default parameter values are used to initialize the functions with default values, default function parameters are the parameters that are provided a default value when we declare the JavaScript function. When no value or undefined is passed in the function, a default value is being set for the function, which is known as the default parameter. It allows the named parameters to get initialized with a default value.
### Arrow function and it's benefit.
Arrow Functions are a very interesting feature in ES6. It provides a more concise syntax for writing function expressions by opting out the function and returning keywords. The shorthand syntax which is used for writing ES6 functions is arrow functions.
The arrow function's definition has consisted of parameters, that are followed by an arrow (=>), and the function's body. Another name of the arrow function is ‘fat arrow'. We won't be able to employ them as constructors. Some benefits of arrow functions are-
It reduces the size of the code.
For a single-line function, the return statement is optional.
Bind the context lexically.
When should one not use arrow functions?
One should not use the arrow functions in the given below cases-
Function Hoisting, Named Functions: As arrow functions are anonymous, we cannot use them when we want function hoisting or when we want to use named functions.
Callback functions with dynamic context.
Since arrow functions don’t have ‘’this’’ value of their own and they depend on their outer context, it uses the value of the enclosing lexical scope.
Too short syntax
Avoid using the arrow function for object methods, event handlers, prototype methods, and functions that use the arguments object.
Top comments (0)