Answer:
The simplest way to create a cookie is to assign a string value to the
object.
ex: document.cookie = "key1 = value1; key2 = value2; expires = date";
Q35. How To Read A Cookie Using JavaScript?
Answer:
To read a Cookie, we have to access the value of the object. This string maintains a list of pairs that is separated with semicolons.
Where, “name” is the name of a cookie and “value” is its string value.
Q36. How To Delete A Cookie Using JavaScript?
Answer:
To delete a Cookie, we have to set its expiry date to a time that occurred in the past. If attempts
are made to read a deleted Cookie then, nothing is returned.
Q37. How Do You Submit A Form Using JavaScript?
Answer:
document.forms[0].submit();
Q38. Why Should You Not Prefer To Use Global Variables In JavaScript And How Can You Prevent It?
Answer:
The principal issue in using a global variable is that someone else can create another variable with the same name. And you may not know it until the duplicate could overwrite the value of your variable.
- Create a single global variable that holds all your other variables.
- Enclose all of your code in a self-executing method/function so that any variable declared inside remain in the function scope.
Q39. What Are The Different Objects Used In JavaScript?
Answer:
Window Object. Document Object. Form Object.
Q40. What Do We Achieve By Deferring The Loading Of JavaScript?
Answer:
By deferring the loading of JavaScript, we instruct the browser to load the script, only after the loading of the web page(DOM) has finished. It reduces the loading time of the webpage, and it gets displayed faster.
Q41. What Are Event Handlers In JavaScript And How To Use Them?
Q42. What Does A JavaScript Function Result When It Has No Return Statement?
Answer:
If a function doesn’t use the return statement, then it returns undefined
as the return value.
Q43. What Is EncodeURI() Function?
Answer:
The encodeURI() function is used to encode a URI. This function encodes all special characters, except these < , / ? : @ & = + $ #>.
Q44. How Does The Differ From <[]> While Creating A JavaScript Array?
Answer:
Both the and <[]> works almost the same in JavaScript.
If we use them as is (i.e., without any argument) to create an array object, then they will result in an array object of zero length. Also, if we pass a string or a list of strings as arguments, even then the result will be similar.
Q45. How Will You Replace All Occurrences Of A String In JavaScript?
Answer:
We can use String’s method to substitute any string.
Q46. How can you read properties of an Object in JavaScript?
Answer:
You can write and read properties of an object using the dot notation
Q47. How to read elements of an array in JavaScript?
Answer:
An array has a length property that is useful for iteration. We can read
elements of an array as follows
var x = [1, 2, 3, 4, 5];
for (var i = 0; i < x.length; i++) {
// Do something with x[i]
}
Q48. What is a named function in JavaScript? How to define a named function?
Answer:
A named function has a name when it is defined. A named function can
be defined using function keyword as follows −
function named(){
// do some stuff here
}
Q49. How many types of functions JavaScript supports?
Answer:
A function in JavaScript can be either named or anonymous.
Q50. Can you assign a anonymous function to a variable?
Answer:
Yes! An anonymous function can be assigned to a variable.
Q51. Can you pass a anonymous function as an argument to another function?
Answer:
Yes! An anonymous function can be passed as an argument to another function.
Q52. What is arguments object in JavaScript?
Answer:
JavaScript variable arguments represents the arguments passed to a function.
Q53. How can you get the type of arguments passed to a function?
Answer:
Using typeof operator, we can get the type of arguments passed to a
function. For example −
function func(x){
console.log(typeof x, arguments.length);
}
func(); //==> "undefined", 0
func(1); //==> "number", 1 func("1", "2", "3"); //==> "string", 3
Q54. How can you get the total number of arguments passed to a function?
Answer:
Using arguments.length property, we can get the total number of arguments passed to a function.
Q55. How can you get the reference of a caller function inside a function?
Answer:
The arguments object has a callee property, which refers to the function you're inside of. For example −
function func() {
return arguments.callee;
}
func(); // ==> func
Q56. Which type of variable among global and local, takes precedence over other if names are same?
Answer:
A local variable takes precedence over a global variable with the same
name.
Q57. What is callback?
Answer:
The callback function is a mechanism to send one function to another function as an argument; i.e., passing func as an argument to another function.
Q58. Which built-in method returns the character at the specified index?
Answer:
charAt() method returns the character at the specified index.
Q59. Which built-in method combines the text of two strings and returns a new string?
Answer:
concat() method returns the character at the specified index.
Q60. Which built-in method calls a function for each element in the array?
Answer:
forEach() method calls a function for each element in the array.
Q61. Which built-in method returns the index within the calling String object of the first occurrence of the specified value?
Answer:
indexOf() method returns the index within the calling String object of the first occurrence of the specified value, or −1 if not found.
Q62. Which built-in method returns the length of the string?
Answer:
length() method returns the length of the string.
Q63. Which built-in method removes the last element from an array and returns that element?
Answer:
pop() method removes the last element from an array and returns that element.
Q64. Which built-in method adds one or more elements to the end of an array and returns the new length of the array?
Answer:
push() method adds one or more elements to the end of an array and returns the new length of the array.
Q65. Which built-in method reverses the order of the elements of an array?
Answer:
reverse() method reverses the order of the elements of an array −− the
first becomes the last, and the last becomes the first.
Q66. Which built-in method sorts the elements of an array?
Answer:
sort() method sorts the elements of an array.
Q67. Which built-in method returns the characters in a string beginning at the specified location?
Answer:
substr() method returns the characters in a string beginning at the specified location through the specified number of characters.
Q68. Which built-in method returns the calling string value converted to lower case?
Answer:
toLowerCase() method returns the calling string value converted to lower case.
Q69. Which built-in method returns the calling string value converted to upper case?
Answer:
toUpperCase() method returns the calling string value converted to upper case.
Q70. Which built-in method returns the string representation of the number's value?
Answer:
toString() method returns the string representation of the number's
value.
Q71. How typeof operator works?
Answer:
The typeof is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand.
The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean value and returns true or false based on the evaluation.
Q72. What typeof returns for a null value?
Answer:
It returns "object".
Q73. Can you access Cookie using javascript?
Answer:
JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookie or cookies that apply to the current web page.
Q74. How to redirect a url using JavaScript?
Answer:
This is very simple to do a page redirect using JavaScript at client side. To redirect your site visitors to a new page, you just need to add a line in your
head section as follows −
Q75. How to print a web page using javascript?
Answer:
JavaScript helps you to implement this functionality using print function of window object. The JavaScript print function window.print() will print the current web page when executed.
Q76. What is Date object in JavaScript?
Answer:
The Date object is a datatype built into the JavaScript language. Date objects are created with the new Date( ).
Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.
Q77. What is Number object in JavaScript?
Answer:
The Number object represents numerical date, either integers or floating-point numbers. In general, you do not need to worry about Number objects because the browser automatically converts number literals to instances of the number class.
Syntax −
Creating a number object −
var val = new Number(number);
Q78. How to handle exceptions in JavaScript?
Answer:
The latest versions of JavaScript added exception handling capabilities. JavaScript implements the try...catch...finally construct as well as the throw operator to handle exceptions.
You can catch programmer-generated and runtime exceptions, but you cannot catch JavaScript syntax errors.
Q79. What is purpose of onError event handler in JavaScript?
Answer:
The onerror event handler provides three pieces of information to identify the exact nature of the error
- Error message − The same message that the browser would display for
the given error.
- URL − The file in which the error occurred.
- Line number − The line number in the given URL that caused the error.
Q80. What is the difference between JavaScript and Jscript?
Answer:
Both JavaScript and Jscript are almost similar. JavaScript was developed by Netscape. Microsoft reverse engineered Javascript and called it JScript.
Q81. What are the boolean operators supported by JavaScript?
Answer:
And Operator: && Or Operator: || Not Operator: !
Q82. How will you get the Checkbox status whether it is checked or not? var status =
Answer:
document.getElementById('checkbox1').checked; alert(status);
will return true or false.
Q83. If an array with name as "names" contain three elements, then how will you print the third element of this array?
Answer:
Print third array element document.write(names[2]);
Q84. What does isNaN function do?
Answer:
It returns true if the argument is not a number.
Q85. What is the use of Math Object in JavaScript?
Answer:
The math object provides you properties and methods for mathematical constants and functions.
Q86. How do you change the style/class on any element using JavaScript?
Answer:
document.getElementById("myText").style.fontSize = "10";
-or-
document.getElementById("myText").className = "anyclass";
Q87. Does JavaScript support foreach loop?
Answer:
JavaScript 1.6(ECMAScript 5th Edition) support foreach loop
Q88. Name any two JavaScript functions which are used to convert nonnumeric values into numbers?
Answer:
Number() parseInt() parseFloat()
Q89. What is the definition of prompt box?
Answer:
A prompt box is a box which allows the user to enter input by providing a text box. The prompt() method displays a dialog box that prompts the visitor for input. A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either “OK” or “Cancel” to proceed after entering an input value.
Q90. What are the disadvantages of using inner HTML in JavaScript?
Answer:
- The process of using inner HTML is much slower than the rest of the variables as its content is slowly built into different elects and takes time to get re-parsed.
- While using the inner HTML, the content gets replaced in JavaScript.
- Appending to inner HTML can't be used properly.
- Using inner HTML can also break the document of the JavaScript. Since no validation is required by it, any type of valid inner HTML can be used. Even broken HTML can also be used and this can cause problems.
- The old content also gets easily replaced.
Q91. What are the meanings of undefined variables and undeclared variables?
Answer:
Undeclared variables are those that do not exist in a program and are not declared. If the program tries to read the value of an undeclared variable, then a runtime error is encountered. Undefined variables are those that are declared in the program but have not been given any value. If the program tries to read the value of an undefined variable, an undefined value is returned.
Q92. Define the types of functional components which is available in JavaScript programming language?
Answer:
- Nested functions - As the name suggests, nested functions are the ones which are included in other functions. They are basically called each time when the invoked functions are provided.
- First class functions - These are the types of functions which are used as first-class objects. Such functions can be used as arguments and opinions against other types of functions. The data structures can store the values of such functional components and the variables can also be stored properly in the data structures.
Q93. How to declare a private and a public member?
Answer:
Private members are declared using var keyword and constructor function.
Function Student (name, roll){ var id= ABCD123;
this.name = name; this.roll = roll;
}
Q94. Define Screen Objects. What is its proper?
Answer:
Screen Objects are used to read a different type of information from the screen of the client.
Q95. How can one handle exceptions in JavaScript?
Answer:
The operators used to handle exceptions in JavaScript programming language are trying, catch and construct. These are the latest versions.
Q96. What is constructor property?
Answer:
Constructor property of an object maintains a reference to its creator function.
Q97. How to call other class methods?
Answer:
Using call () and apply () method
call (): It is used to calls a function with a given this value and arguments provided individually.
fun.call(thisArg[, arg1[, arg2[, ...]]])
apply (): It is used to call a function with a given this value and arguments provided as an array.
fun.apply(thisArg, [argsArray])
Q98. How many types of objects are there in JavaScript? Define them?
Answer:
- Date object - q.no 76
- Number object - q.no 78
Q99. Explain method overriding with an Example?
Answer:
We can override any inbuilt method of JavaScript by declaring its definition again. The existing definition is accessible at override by the Prototype property.
Q100. What is the meaning of continuing and break statements?
Answer:
Continue statements are the ones which continue from the next loop with the next set of statements. On the other hand, break statements are the ones that start from the current existing loop.
Q101. How to inherit form a class?
Answer:
Inheritance can be achieved in JavaScript using Prototype property. Step1:
Child class prototype should point to parent class object.
.prototype = new (); Step2:
Reset the child class prototype constructor to point self. .prototype . constructor = student;
Q102. What is the difference between Attributes and Property?
Answer:
Attributes- provide more details on an element like id, type, value etc.
Property- is the value assigned to the property like type=”text”, value=’Name’ etc.
Q103. List out the different ways an HTML element can be accessed in a JavaScript code.
Answer:
- getElementById(‘idname’): Gets an element by its ID name
- getElementsByClass(‘classname’): Gets all the elements that have the
given classname.
- getElementsByTagName(‘tagname’): Gets all the elements that have
the given tag name.
- querySelector(): This function takes css style selector and returns the first selected element.
Q104. Explain how to detect the operating system on the client machine?
Answer:
In order to detect the operating system on the client machine, the navigator.platform string (property) should be used.
Q105. Whether JavaScript has concept level scope?
Answer:
No. JavaScript does not have concept level scope. The variable declared inside the function has scope inside the function.
onDocumentReady loads the code just after the DOM is loaded. This allows early manipulation of the code.
Q106. What is a Typed language?
Answer:
Typed Language is in which the values are associated with values and not with variables. It is of two types:
Dynamically: in this, the variable can hold multiple types; like in JS a variable can take number, chars.
Statically: in this, the variable can hold only one type, like in Java a variable declared of string can take only set of characters and nothing else.
Q107. What is the difference between Local storage & Session storage?
Answer:
Local Storage – The data is not sent back to the server for every HTTP request (HTML, images, JavaScript, CSS, etc) – reducing the amount of traffic between client and server. It will stay until it is manually cleared through settings or program.
Session Storage – It is similar to local storage; the only difference is while data stored in local storage has no expiration time, data stored in session storage gets cleared when the page session ends. Session Storage will leave when the browser is closed.
Q108. Name some of the JavaScript Frameworks
Answer:
Angular React Vue
Q109. What is the difference between window & document in JavaScript? Window:
Answer:
JavaScript window is a global object which holds variables, functions, history, location.
Document:
The document also comes under the window and can be considered as the property of the window.
110. What is the difference between innerHTML & innerText?
Answer:
innerHTML – It will process an HTML tag if found in a string innerText – It will not process an HTML tag if found in a string
Q111. What is an event bubbling in JavaScript?
Answer:
When an event is fired on an HTML element, the execution starts from that event and goes to its parent element. From there, the execution passes to its parent element and so on till the body element.
stop:
Q112. What is NaN in JavaScript?
Answer:
NaN is a short form of Not a Number. Since NaN always compares unequal to any number, including NaN, it is usually used to indicate an error condition for a function that should return a valid number. When a string or
something else is being converted into a number and that cannot be done, then we get to see NaN.
Q113. How do JavaScript primitive/object types passed in functions?
Answer:
One of the differences between the two is that Primitive Data Types are passed By Value and Objects are passed By Reference.
By Value means creating a COPY of the original. Picture it like twins: they are born exactly the same, but the first twin doesn’t lose a leg when the second twin loses his in the war.
By Reference means creating an ALIAS to the original. When your Mom calls you “Pumpkin Pie” although your name is Margaret, this doesn’t suddenly give birth to a clone of yourself: you are still one, but you can be called by these two very different names.
Q114. How can you convert the string of any base to integer in JavaScript?
Answer:
The parseInt() function is used to convert numbers between different bases. It takes the string to be converted as its first parameter, and the second parameter is the base of the given string.
Q115. What are Exports & Imports?
Answer:
Imports and exports help us to write modular JavaScript code. Using Imports and exports we can split our code into multiple files.
Q116. What happens when the recursion calling is applied on two functions?
Answer:
The calling of recursion is possible in two functions, but the call comes to an end after some time.
Q117. Explain the terms synchronous and asynchronous code.
Answer:
The synchronous code is something that should be finished before anything else can happen, or in other words, the synchronous code is blocking. And the Asynchronous code is something in which actions can happen and is not dependent on other actions- in other words, it is non-blocking.
Q118. Name the different types of pop up boxes in Javascript.
Answer:
There are three types of pop up boxes in Javascript (i) alert() provides some information to the user with just an OK button (ii) confirm() asks a question to the user with two options Ok and cancel, and (iii) prompt() takes an input from the user.
Q119. What is the difference between typeof and instanceof operators in Javascript?
Answer:
The typeof operator returns a string of what type the operand is.
Whereas, the instanceof operator does not work with primitive data types; but works with objects and checks on what type the object is.
Q120. What is DOM?
Answer:
DOM (Document Object Model) is an object-oriented representation of the HTML elements. All the elements (or nodes) are part of window.document.
121. Expand BOM and explain it.
Answer:
BOM stands for Browser Object Model. Using BOM interaction with a browser is possible. Default object of the browser is a window.
Q122. What is the difference between textContent and innerText?
Answer:
Let us have a paragraph element and a span element in it as a child element.
some text and a span tag hidden
<\span>in it
Now, if the following two steps would result in the following- console.log(document.querySelector('p').textContent); gives some text and a span tag hidden in it.
console.log(document.querySelector('p').innerText); gives some text and a in it.
Q123. What is the difference between HTMLCollection and NodeList?
Answer:
The functions querySelectorAll() returns NodeList in which the forEach can be used directly to traverse the elements. Whereas, the getElementsByClassName() or getElementsByTagName() returns an HTMLCollection, which does not have a forEach by default.
Q124. How can an HTMLCollection be traversed?
Answer:
The HTMLCollection by default does not have forEach, it needs to be converted into an array (eleName = Array.from(eleName)) and then the traversal is possible using forEach.
Q125. What is the difference between childNode and children?
Answer:
A childNode, when used returns a NodeList. Whereas, children, when used; returns an HTMLCollection.
Q126. What is the difference between firstChild and firstElementChild?
Answer:
A firstChild when used returns the first node. It could be an HTML element or even a space, or a new line. Whereas, firstElementChild, when used returns the first HTML element only.
Q127. Name the two functions that are used to create an HTML element dynamically.
Answer:
To generate an html element dynamically, we need to use two functions:
and
(i) var ele = createElement ('elementName'), which creates an element
(ii) parentElement.appendChild(ele)
Q128. What is the difference between remove() and removeChild()?
Answer:
The remove() function just removes the element. Whereas, the removeChild() returns the deleted element.
Q129. What is the use of history object?
Answer:
By using the history object you can change or switch back to history pages or forward from current page to another page.
Q130. List out all the falsifying tokens in Javascript.
Answer:
There are 6 tokens that falsify in Javascript and they are false, null, undefined, 0, NaN.
Q131. What is Currying in Javascript?
Answer:
A partial invocation of a Javascript function is called Currying. Few arguments of a function are processed and a function is returned. Few more arguments are added by the returning function.
Q132. When do we use JSON.stringify()?
Answer:
The JSON.stringify() method is used to convert a Javascript data to a string.
Q133. What is the difference between ViewState and SessionState?
Answer:
'ViewState' is specific to a page in a session.
'SessionState' is specific to user specific data that can be accessed across all pages in the web application.
Q134. Explain how to read and write a file using JavaScript?
Answer:
There are two ways to read and write a file using JavaScript Using JavaScript extensionsl Using a web page and Active X objects
Q135.138. Write the point of difference between web-garden and a web-farm?
Answer:
Both web-garden and web-farm are web hosting systems. The only difference is that web-garden is a setup that includes many processors in a single server while web-farm is a larger setup that uses more than one server.
Q136. What is namespacing in JavaScript and how is it used?
Answer:
Namespacing is used for grouping the desired functions, variables etc. under a unique name. It is a name that has been attached to the desired functions, objects and properties. This improves modularity in the coding and enables code reuse.
Q137. What is the difference between classical inheritance and prototypal inheritance?
Answer:
Class Inheritance: instances inherit from classes (like a blueprint - a description of the class), and create sub-class relationships:
hierarchical class taxonomies. Instances are typically instantiated via constructor functions with the new keyword. Class
inheritance may or may not use the class keyword from ES6. Prototypal Inheritance: instances inherit directly from other objects.
Instances are typically instantiated via factory functions or Object.create(). Instances may be composed from many different objects, allowing for easy selective inheritance.
Q138. Map
Answer:
It creates a new array element with the result of calling a function with every array element
var numbers = [2, 4, 6, 8]
var half = numbers.map(x=>x/2);
Q139. Filters
Answer:
Its create a new array with all element that pass the text implemented by the provider function.
const words = ["spray", "limit", "elite", "destruction"]; const longwords = words.filter(word=>word.length>6);
Q140. Reduce
Answer:
const total = [0, 1, 2, 3].reduce((sum,value)=>sum+value, )