JavaScript Questions On Interview- Must Remember Flashcards

1
Q

What is the difference between “let” and “const” ?

A

Let is to declare variables that can be reassigned later
The value can be changed later.

Const is to declare constants that are not to be changed
It value cannot be changed.

Const needs to be initialized when declared

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between == and === in JS?

A

== does not take type into consideration
“23” == 23 is true

=== takes data type into consideration
“23” === 23 is false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can an array be resized? ( in Java, arrays cannot be resized)

A

Yes it can in JavaScript.

JavaScript has dynamic arrays: their size is not predetermined, nor the type of data

There are methods to handle this behavior:
push - adds item at the end and pop - add item from the end
shift - remove items from the beginning and unshift- adds items to the beginning

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain this code: Is this Valid?

var John = {

firstName: 'John',
lastName: 'Smith', 
birthYear: 1992,
family: ['Jane', 'Mark', 'Bob', 'Emily'], 
job: 'teacher', 
isMarried: false, 
};
A

You have an object of John that has multiple properties

This is valid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain this code:

for( var i =0; i < john.length; i++ {

if(typeof john[i] ! == ‘string’)
continue;
console.log(john[i]); }

A

if the object type of john array at index i is not equal to a string, continue to the iteration

console.log(john[i] = writing into the browser console

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain this code:

for( var i =0; i < john.length; i++ {

if(typeof john[i] ! == ‘string’)
break;
console.log(john[i]); }

A

if the object type of john array at index i is not equal to a string, break the loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the equivalent of driver.findElement in JS ?

A

Document.querySelector(locator)

Document.findElementById(locator)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the equivalent of driver.findElement in JQuery?

A

$(locator) . find()

also known as selector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the equivalent of driver.findElements in JS?

A

Document.querySelectorAll(locator)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the equivalent of driver.findElements in JQuery?

A

$(locator). find()

also known as selector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do I execute javascript via selenium ?

A

javascriptExecutor and write the JS code in a string

Js.executeScript(“arguments[0].click();”,
js.executeScript(“alert(‘Welcome TbSoftwareTestingMaterial’);”);

Just use predefined interface named ‘Java Script Executor’. We need to import the below package in the script.

Then executeScript( String
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly