A job interview is a situation that we all face in our career path. The interview is more important, especially for programming jobs compared to other jobs. The programmer must be able to show all his technical abilities and his motivation and enthusiasm for programming and cooperation with a company in a job interview.
In this article, the most important JavaScript programmer interview questions for novice and beginner programmers have been discussed. So, if you are one of those people who have just attended a JavaScript training course and want to enter the job market, this article is for you.
Table of Contents
Name and explain the types of data types in JavaScript.
Data types in JavaScript are divided into two categories:
- Primitive:
Primitive data is data that is stored directly in memory. This type of data includes the following:
- Numbers,
- Strings,
- Boolean values,
- The values are undefined and
- Non-primitive:
Non-primitive data is a set of primitive data that are stored as references in memory and refer to the data storage location instead of keeping the actual value of that variable. This data includes the following:
- Arrays,
- Objects
Also, to know the data type of a variable in JavaScript, the type of operator can be used.
2. Explain Hoisting in JavaScript.
Hoisting in JavaScript means that variables and functions are moved to the top of the code by default. In other words, you can bring the variable or function that you want to use in the next sections of the code before defining it.
For example, if you include a variable in the code before it is defined, JavaScript will automatically move it to the top of the code. As a result, you can use that variable even before it is defined and initialized in your code.
This principle also applies to functions. You can call a function even if you add the function definition later in the code.
The important thing is that in hoisting, only the definition of variables is moved to the top of the code, not their initialization. In other words, the value you assign to a variable is not passed to the top of the code at the same time the variable is defined. Therefore, to avoid unwanted problems and to create more readable code, it is better to define variables before using them.
3. What is the difference between two operators “==” and “===”?
“==” and “===” operators are both comparison operators. But the main difference between them is that “==” is used to compare values, while “===” is used to compare both values and data types.
4. What is the difference between var and let in JavaScript?
The keyword “var” was used from the beginning of the introduction and development of the JavaScript language. The keyword “let” was added to this language in 2015.
“var” variables are of function scope type, that is, they can be accessed at any point inside the function. But in “let”, the scope of variables is limited to the block in which they are defined.
In the ECMAScript 2015 (or ES6) standard, both “let” and “var” are hoisted, but “var” variables are initially initialized with the value “undefined”. In the case of “let”, they are in the “temporal dead zone” from the beginning until the initialization is done. So, if you use “let” variables before defining them in a block, you will encounter a reference error.
5. What does NaN mean in JavaScript?
The NaN attribute in JavaScript means Not-a-Number. This means a value that is not recognized as a number. To check whether a value is NaN or not, the isNaN() function is used.
Note: The isNaN() function converts the given value to a number and then compares it with NaN.
for example:
- The isNaN(“Hello”) function returns true because “Hello” is treated as an invalid text value and converted to NaN.
- The isNaN(345) function returns false because 345 is a valid number and not invalid.
- “isNaN(‘1’)” function returns “false” output because “1” as a text value is converted to number data type, which is equivalent to 0.
- The isNaN(true) function returns false because true is converted to a number datatype, which is equivalent to 1.
- “isNaN(false)” function returns “false” output because the false value is converted to the number data type, which is equivalent to 0.
- “isNaN(undefined)” function returns true because undefined is not converted to number data type and is converted to NaN.
6. What is the difference between exec() and test()?
In JavaScript, two methods named `()exec` and `()test` are used to work with return expressions (RegExp).
`()exec`: Using this method, we can search a string for a specific pattern. If found, the matched pattern itself is returned; Otherwise, it returns `null’ if no match is found.
“test()”: This method is also used to check a string for a specific pattern. with the difference that the result is a boolean value, in the sense that it returns “true” if the pattern is found in the string, and “false” otherwise.
7. Explain the concept of the Scope and Scope chain in JavaScript.
In JavaScript, the concept of “Scope” or scope determines what variables and functions are available in different parts of the code.
In JavaScript, there are three types of Scope:
- Global Scope: Variables or functions that are defined in the global namespace have a global scope. This means that all variables and functions can be accessed from any point in the code.
- Local or Function Scope: Variables or functions that are defined inside a function have a local or function scope. That is, all the variables and functions defined in a function are accessible only inside that function.
- Block Scope: Block scope is related to variables that are defined using let and const. Variables defined with var do not have block scope. This concept tells us that any variable defined inside a { } block can only be accessed inside that block.
Scope Chain: The JavaScript engine uses Scope to find variables. In other words, if the variable is not found in the local or function scope, it tries to find the variable in the external domain. If the variable is not found in the external domain, it goes to the global domain, and if it is not found there, a reference error is known.
8. Explain the concept of Closure in JavaScript.
Closure in JavaScript is created when an inner function uses an outer function and variables of the outer function.
9. What does the concept of Object Prototypes mean in JavaScript?
In JavaScript, every object (Object) is inherited from a certain format called “Prototype”. This Prototype contains common features and behaviors that all objects use. In this way, if properties and functions are defined in the Prototype, all objects that are created from that Prototype will have these properties and functions.
10. Explain the types of errors in JavaScript.
In JavaScript, there are two types of errors:
- Syntax Error: These types of errors occur when there are mistakes or spelling problems in the code that cause the program not to run at all or the running program is interrupted halfway. In these errors, error messages are displayed as a result of incorrect operation.
- Logical Error: These types of errors occur when the logic of the program is wrong. In this case, the program runs without problems, but the output results are incorrect. Correcting these types of errors is sometimes more difficult than syntax errors because the program does not display an error.