JavaScript Questions On Interview- Must Remember Flashcards
What is the difference between “let” and “const” ?
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
What is the difference between == and === in JS?
== does not take type into consideration
“23” == 23 is true
=== takes data type into consideration
“23” === 23 is false
Can an array be resized? ( in Java, arrays cannot be resized)
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
Explain this code: Is this Valid?
var John = {
firstName: 'John', lastName: 'Smith', birthYear: 1992, family: ['Jane', 'Mark', 'Bob', 'Emily'], job: 'teacher', isMarried: false, };
You have an object of John that has multiple properties
This is valid
Explain this code:
for( var i =0; i < john.length; i++ {
if(typeof john[i] ! == ‘string’)
continue;
console.log(john[i]); }
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
Explain this code:
for( var i =0; i < john.length; i++ {
if(typeof john[i] ! == ‘string’)
break;
console.log(john[i]); }
if the object type of john array at index i is not equal to a string, break the loop
What is the equivalent of driver.findElement in JS ?
Document.querySelector(locator)
Document.findElementById(locator)
What is the equivalent of driver.findElement in JQuery?
$(locator) . find()
also known as selector
What is the equivalent of driver.findElements in JS?
Document.querySelectorAll(locator)
What is the equivalent of driver.findElements in JQuery?
$(locator). find()
also known as selector
How do I execute javascript via selenium ?
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);