JavaScript Tricky Interview Questions And Answers are asked for Angular, Typescript, Node js, and React js interviews. The interviewer checks the basic knowledge of JavaScript.
How to initialize an array in JavaScript
We have two ways to initialize an array in JavaScript. First, we can use a square bracket
[]
. or we can use
new Array
keyword.
1 2 3 | var numbers = [23, 45, 56]; //or var numbers = new Array(23,34,35); |
How to find the number of characters of the string in JavaScript?
We use
str.length
to find the character length of the string.
1 2 | var str = "Troposal"; console.log(str.length); |
Which function of an array object removes the first element from an array and returns that removed element?
We use
shift()
function to remove the first element and this function also returns that removed element.
1 2 | var arr = [2, 3, 4, 5]; console.log(arr.shift()); |
What is the syntax of removing the white space at the beginning and end of any string?
We use
trim
function to remove the white space from the beginning and from the end of the string.
1 2 | var name = " Troposal "; console.log(name.trim()) |
Predict the output on the console for the following code:
Question 1: What will be the out of the following filter code:
1 | console.log([0,1,2,3,undefined].filter(result=>result)) |
Check the filter JavaScript Array method.
Question 2: Predict the output:
1 | console.log(undefined||null||''||0||'hello'||23) |
Question 3: What
typeof
return in the below code?
1 2 3 4 | function aaa() { return {test:1}; } console.log(typeof aaa()) |
We will get
object
in the console because the function returns an object.
Question 4: What will be the output of the below console code:
1 | console.log(String('Troposal') === 'Troposal') |
It will return
true
in the console.
Question 5: Predict the answer:
1 2 | a = 5 + "5"; console.log(a); |
It will show
55
in the console because
5
and
"5"
will be concatenated.
Question 6: What will be the output of the below console code?
1 | console.log([]+[]+'RRT'.split('')); |
It will show
R,R,T
in the console.
Question 7: Predict the output:
1 2 | var siteName = "Troposal"; console.log(typeof siteName); |
It will return
string
because the variable is storing the string value.
Question 8: Predict the answer:
1 | console.log("3"-"3"); |
The output will show
0
in the console.
Question 9: Predict the length of the array.
1 2 3 | var arr = ['Angular', 'TypeScript']; arr.script = 'JavaScript'; console.log(arr.length); |
The output will be
2
.
Question 10: What
typeof
return in the below code?
1 2 3 4 | function test() { return a = 10; } console.log(typeof test()); |
It will show
number
in the console because we are assigning the number in the variable(a) and returning that variable in the function.
Question 11: Predict the output of the below console log:
1 2 3 4 | var a = []; var b = []; console.log(a==b); console.log({}=={}); |
Both console logs return
false
results because of comparing two different arrays or objects.
Question 12: Guess the output of the below JavaScript code:
1 2 3 4 5 6 7 8 | let x = true; setTimeout(()=>{ x = false; }); if(x) { console.log("Hi"); } |
The output will be “Hi”.
Question 13: What will be the output of the below JavaScript code:
1 2 3 | var a = '30'; var b = a = 20; console.log(a+b); |
Output: 40
Conclusion:
These are the JavaScript Tricky Interview Questions And Answers. Read those questions and answers before attending an interview related to JavaScript, Angular, Node, and React.