Tổng hợp các câu hỏi phỏng vấn Javascript thường gặp nhất - Phần 1


JavaScript, được tạo bởi Brendan Eich vào năm 1995, là một trong những ngôn
ngữ phát triển web được sử dụng rộng rãi nhất. Ban đầu nó được thiết kế để xây
dựng các trang web động. Tập lệnh là một chương trình JS có thể được thêm vào
HTML của bất kỳ trang web nào. Khi trang tải, các tập lệnh này sẽ tự động thực
thi.



Một ngôn ngữ ban đầu được thiết kế để xây dựng các trang web động giờ đây có
thể chạy trên máy chủ và trên hầu hết mọi thiết bị đã cài đặt Công cụ
JavaScript.



Sau HTML và CSS, JavaScript là công nghệ web lớn thứ ba. JavaScript là một
ngôn ngữ kịch bản có thể được sử dụng để xây dựng các ứng dụng trực tuyến và
di động, máy chủ web, trò chơi, v.v. JavaScript là ngôn ngữ lập trình hướng
đối tượng được sử dụng để tạo các trang web và ứng dụng. Nó được tạo ra với
mục đích được sử dụng trong trình duyệt. Thậm chí ngày nay, phiên bản
JavaScript phía máy chủ được gọi là Node.js có thể được sử dụng để tạo ứng
dụng trực tuyến và ứng dụng dành cho thiết bị di động, ứng dụng thời gian
thực, ứng dụng phát trực tuyến và trò chơi điện tử. Các khung Javascript,
thường được gọi là thư viện sẵn có, có thể được sử dụng để xây dựng các chương
trình dành cho máy tính để bàn và thiết bị di động. Các nhà phát triển có thể
tiết kiệm rất nhiều thời gian cho các công việc lập trình đơn điệu bằng cách
sử dụng các thư viện mã này, cho phép họ tập trung vào công việc phát triển
sản xuất.

Các bài viết liên quan:






Tổng hợp các câu hỏi phỏng vấn Javascript thường gặp nhất - Phần 1





JavaScript Interview Questions for Freshers



1. What are the different data types present in javascript?



To know the type of a JavaScript variable, we can use the typeof operator.



1. Primitive types





  • String - It represents a series of characters and is written
    with quotes. A string can be represented using a single or a double
    quote.





Example :







var str = "Vivek Singh Bisht"; //using double quotes
var str2 = 'John Doe'; //using single quotes









  • Number - It represents a number and can be written with or
    without decimals.





Example :





var x = 3; //without decimal
var y = 3.6; //with decimal








  • BigInt - This data type is used to store numbers which are above
    the limitation of the Number data type. It can store large integers and
    is represented by adding “n” to an integer literal.





Example :





var bigInteger =  234567890123456789012345678901234567890;








  • Boolean - It represents a logical entity and can have only two
    values : true or false. Booleans are generally used for conditional
    testing.





Example :





var a = 2;
var b = 3;
var c = 2;
(a == b) // returns false
(a == c) //returns true








  • Undefined - When a variable is declared but not assigned, it has
    the value of undefined and it’s type is also undefined.





Example :





var x; // value of x is undefined
var y = undefined; // we can also set the value of a variable as undefined







  • Null - It represents a non-existent or a invalid value.





Example :





var z = null;








  • Symbol - It is a new data type introduced in the ES6 version of
    javascript. It is used to store an anonymous and unique value.



Example :





var symbol1 = Symbol('symbol');







  • typeof of primitive types :








typeof "John Doe" // Returns "string"
typeof 3.14 // Returns "number"
typeof true // Returns "boolean"
typeof 234567890123456789012345678901234567890n // Returns bigint
typeof undefined // Returns "undefined"
typeof null // Returns "object" (kind of a bug in JavaScript)
typeof Symbol('symbol') // Returns Symbol






2. Non-primitive types





  • Primitive data types can store only a single value. To store multiple
    and complex values, non-primitive data types are used.

  • Object - Used to store collection of data.





Example:





// Collection of data in key-value pairs
var obj1 = {
x: 43,
y: "Hello world!",
z: function(){
return this.x;
}
}
// Collection of data as an ordered list

var array1 = [5, "Hello", true, 4.1];







Note- It is important to remember that any data type that is not a
primitive data type, is of Object type in javascript.






2. Explain Hoisting in javascript.



Hoisting is the default behaviour of javascript where all the variable and
function declarations are moved on top.





Explain Hoisting in javascript.





This means that irrespective of where the variables and functions are
declared, they are moved on top of the scope. The scope can be both local
and global.



Example 1:





hoistedVariable = 3;
console.log(hoistedVariable); // outputs 3 even when the variable is declared after it is initialized
var hoistedVariable;





Example 2:





hoistedFunction();  // Outputs " Hello world! " even when the function is declared after calling
function hoistedFunction(){
console.log(" Hello world! ");
}





Example 3:





// Hoisting takes place in the local scope as well
function doSomething(){
x = 33;
console.log(x);
var x;
}






doSomething(); // Outputs 33 since the local variable “x” is hoisted inside
the local scope






Note - Variable initializations are not hoisted, only variable
declarations are hoisted:







var x;
console.log(x); // Outputs "undefined" since the initialization of "x" is not hoisted
x = 23;







Note - To avoid hoisting, you can run javascript in strict mode by using
“use strict” on top of the code:






"use strict";
x = 23; // Gives an error since 'x' is not declared
var x;







3. Why do we use the word “debugger” in javascript?



The debugger for the browser must be activated in order to debug the code.
Built-in debuggers may be switched on and off, requiring the user to report
faults. The remaining section of the code should stop execution before
moving on to the next line while debugging.






4. Difference between “ == “ and “ === “ operators.



Both are comparison operators. The difference between both the operators is
that “==” is used to compare values whereas, “ === “ is used
to compare both values and types.






var x = 2;
var y = "2";
(x == y) // Returns true since the value of both x and y is the same
(x === y) // Returns false since the typeof x is "number" and typeof y is "string"







5. Difference between var and let keyword in javascript.


Some differences are 






  • From the very beginning, the 'var' keyword was used in JavaScript
    programming whereas the keyword 'let' was just added in 2015.


  • The keyword 'Var' has a function scope. Anywhere in the function, the
    variable specified using var is accessible but in ‘let’ the scope of a
    variable declared with the 'let' keyword is limited to the block in
    which it is declared. Let's start with a Block Scope.


  • In ECMAScript 2015, let and const are hoisted but not initialized.
    Referencing the variable in the block before the variable declaration
    results in a ReferenceError because the variable is in a "temporal dead
    zone" from the start of the block until the declaration is processed.










6. What is NaN property in JavaScript?



NaN property represents the “Not-a-Number” value. It indicates a
value that is not a legal number.



typeof of NaN will return a Number.



To check if a value is NaN, we use the isNaN() function,






Note- isNaN() function converts the given value to a Number type, and then
equates to NaN.






isNaN("Hello")  // Returns true
isNaN(345) // Returns false
isNaN('1') // Returns false, since '1' is converted to Number type which results in 0 ( a number)
isNaN(true) // Returns false, since true converted to Number type results in 1 ( a number)
isNaN(false) // Returns false
isNaN(undefined) // Returns true







7. Explain passed by value and passed by reference.



In JavaScript, primitive data types are passed by value and non-primitive
data types are passed by reference.





For understanding passed by value and passed by reference, we need to
understand what happens when we create a variable and assign a value to it,



var x = 2;


In the above example, we created a variable x and assigned it a value of
“2”. In the background, the “=” (assign operator) allocates some space in
the memory, stores the value “2” and returns the location of the allocated
memory space. Therefore, the variable x in the above code points to the
location of the memory space instead of pointing to the value 2 directly.




Assign operator behaves differently when dealing with primitive and
non-primitive data types,



Assign operator dealing with primitive types:






Tổng hợp các câu hỏi phỏng vấn Javascript thường gặp nhất - Phần 1






var y = 234;
var z = y;






In the above example, the assign operator knows that the value assigned to y
is a primitive type (number type in this case), so when the second line code
executes, where the value of y is assigned to z, the assign operator takes the
value of y (234) and allocates a new space in the memory and returns the
address. Therefore, variable z is not pointing to the location of variable y,
instead, it is pointing to a new location in the memory.





var y = #8454; // y pointing to address of the value 234

var z = y;

var z = #5411; // z pointing to a completely new address of the value 234

// Changing the value of y
y = 23;
console.log(z); // Returns 234, since z points to a new address in the memory so changes in y will not effect z







From the above example, we can see that primitive data types when passed to
another variable, are passed by value. Instead of just assigning the same
address to another variable, the value is passed and new space of memory is
created.



Assign operator dealing with non-primitive types:






Tổng hợp các câu hỏi phỏng vấn Javascript thường gặp nhất - Phần 1






var obj = { name: "Vivek", surname: "Bisht" };
var obj2 = obj;






In the above example, the assign operator directly passes the location of the
variable obj to the variable obj2. In other words, the reference of the
variable obj is passed to the variable obj2.





var obj = #8711;  // obj pointing to address of { name: "Vivek", surname: "Bisht" }
var obj2 = obj;

var obj2 = #8711; // obj2 pointing to the same address

// changing the value of obj1

obj1.name = "Akki";
console.log(obj2);

// Returns {name:"Akki", surname:"Bisht"} since both the variables are pointing to the same address.







From the above example, we can see that while passing non-primitive data
types, the assign operator directly passes the address (reference).



Therefore, non-primitive data types are always passed by reference.






8. What is an Immediately Invoked Function in JavaScript?



An Immediately Invoked Function ( known as IIFE and pronounced as IIFY)
is a function that runs as soon as it is defined.




Syntax of IIFE:






(function(){ 
// Do something;
})();







To understand IIFE, we need to understand the two sets of parentheses that
are added while creating an IIFE :



The first set of parenthesis:






(function (){
//Do something;
})






While executing javascript code, whenever the compiler sees the word
“function”, it assumes that we are declaring a function in the code.
Therefore, if we do not use the first set of parentheses, the compiler throws
an error because it thinks we are declaring a function, and by the syntax of
declaring a function, a function should always have a name.





function() {
//Do something;
}
// Compiler gives an error since the syntax of declaring a function is wrong in the code above.







To remove this error, we add the first set of parenthesis that tells the
compiler that the function is not a function declaration, instead, it’s a
function expression.



The second set of parenthesis:






(function (){
//Do something;
})();






From the definition of an IIFE, we know that our code should run as soon as it
is defined. A function runs only when it is invoked. If we do not invoke the
function, the function declaration is returned:





(function (){
// Do something;
})

// Returns the function declaration






Therefore to invoke the function, we use the second set of parenthesis.





9. What do you mean by strict mode in javascript and characteristics of
javascript strict-mode?



In ECMAScript 5, a new feature called JavaScript Strict Mode allows you to
write a code or a function in a "strict" operational environment. In most
cases, this language is 'not particularly severe' when it comes to throwing
errors. In 'Strict mode,' however, all forms of errors, including silent
errors, will be thrown. As a result, debugging becomes a lot simpler. 
Thus programmer's chances of making an error are lowered.



Characteristics of strict mode in javascript



  1. Duplicate arguments are not allowed by developers.


  2. In strict mode, you won't be able to use the JavaScript keyword as a
    parameter or function name.


  3. The 'use strict' keyword is used to define strict mode at the start of
    the script. Strict mode is supported by all browsers.


  4. Engineers will not be allowed to create global variables in 'Strict
    Mode.








10. Explain Higher Order Functions in javascript.



Functions that operate on other functions, either by taking them as
arguments or by returning them, are called higher-order functions.





Higher-order functions are a result of functions being first-class citizens
in javascript.



Examples of higher-order functions:






function higherOrder(fn) {
fn();
}

higherOrder(function() { console.log("Hello world") });







function higherOrder2() {
return function() {
return "Do something";
}
}
var x = higherOrder2();
x() // Returns "Do something"





Kết thúc



Tất cả những câu hỏi trên đều do mình thu nhặt được ở những nơi mà có những
người có kinh nghiệm truyền lại. Mong là sẽ có ích cho bạn!




Mình sẽ cố gắng tìm thêm những câu hỏi phổ biến hơn nữa và ra tiếp phần 2 cho
mọi người nhé!



Chúc các bạn thành công!



Hiju Blog

I'm HiJu

Post a Comment

Previous Post Next Post