DEV Community is a community of 879,796 amazing developers
Were a place where coders share, stay up-to-date and grow their careers.
Hi Guys Good Day and a Happy New Year !
This is a long one, so bear with me for a second or an hour. In every answer for every question theres an arrow up ↑ link that lets you go back to the List of Questions so that you dont waste time scrolling up and down.
4. What is the use of isNaN function?
isNan function returns true if the argument is not a number; otherwise, it is false.
6. What is negative Infinity?
Breaking within a string statement can be done by using a backslash, ‘,’ at the end of the first line.
The above code is perfectly fine, though not advisable as it hampers debugging.
9. What are undeclared and undefined variables?
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.
10. Write the code for adding new elements dynamically?
11. What are global variables? How are these variable declared?
Global variables are available throughout the length of the code so that it has no scope. The var keyword is used to declare a local variable or object. If the var keyword is omitted, a global variable is declared.
// Declare a global: globalVariable = “Test”;
The problems faced by using global variables are the clash of variable names of local and global scope. Also, it is difficult to debug and test the code that relies on global variables.
12. What is a prompt box?
A prompt box is a box that allows the user to enter input by providing a text box. A label and box will be provided to enter the text or number.
‘This’ keyword refers to the object from where it was called.
Timers are used to execute a piece of code at a set time or repeat the code in a given interval. This is done by using the functions setTimeout, setInterval, and clearInterval.
The setTimeout(function, delay) function is used to start a timer that calls a particular function after the mentioned delay. The setInterval(function, delay) function repeatedly executes the given function in the mentioned delay and only halts when canceled. The clearInterval(id) function instructs the timer to stop.
Timers are operated within a single thread, and thus events might queue up, waiting to be executed.
// for Single line comments and
17. What is === operator?
=== is called a strict equality operator, which returns true when the two operands have the same value without conversion.
20. How can the style/class of an element be changed?
It can be done in the following way:
Variable typing is used to assign a number to a variable. The same variable can be assigned to a string.
This is called variable typing.
The parseInt() function is used to convert numbers between different bases. parseInt() takes the string to be converted as its first parameter. The second parameter is the base of the given string.
To convert 4F (or base 16) to integer, the code used will be –
25. Difference between “==” and “===”?
“==” checks only for equality in value, whereas “===” is a stricter equality test and returns false if either the value or the type of the two variables are different.
Since 3 and 2 are integers, they will be added numerically. And since 7 is a string, its concatenation will be done. So the result would be 57.
27. How to detect the operating system on the client machine?
In order to detect the operating system on the client machine, the navigator. Platform string (property) should be used.
The NULL value is used to represent no value or no object. It implies no object or null string, no valid boolean value, no number, and no array object.
29. What is the function of the delete operator?
The delete keyword is used to delete the property as well as its value.
32. What is the use of Void (0)?
Void(0) is used to prevent the page from refreshing, and parameter “zero” is passed while calling.
Void(0) is used to call another method without refreshing the page.
The following code has to be inserted to achieve the desired effect:
35. What is the difference between an alert box and a confirmation box?
An alert box displays only one button, which is the OK button.
But a Confirmation box displays two buttons, namely OK and cancel.
Escape characters (Backslash) is used when working with special characters like single quotes, double quotes, apostrophes, and ampersands. Place backslash before the characters to make it display.
Cookies are the small test files stored in a computer, and they get created when the user visits the websites to store information that they need. Examples could be User Name details and shopping cart information from previous visits.
The pop() method is similar to the shift() method, but the difference is that the Shift method works at the array’s start. The pop() method takes the last element off of the given array and returns it. The array on which it is called is then altered.
41. What is break and continue statements?
Break statement exits from the current loop.
Continue statement continues with next statement of the loop.
Primitive types are number and Boolean data types. Reference types are more complex types like strings and dates.
43. How can generic objects be created?
Generic objects can be created as:
44. What is the use of a type of operator?
‘Typeof’ is an operator used to return a string description of the type of a variable.
45. Which keywords are used to handle exceptions?
46. Which keyword is used to print the text on the screen?
Document. Write (“Welcome”) is used to print the text–Welcome on the screen.
47. What is the use of the blur function?
Blur function is used to remove the focus from the specified object.
48. What is variable typing?
Variable typing assigns a number to a variable and then assigns a string to the same variable. An example is as follows:
The ‘Navigator. the app version is used to find the operating system’s name in the client machine.
There are three types of errors:
The push method is used to add or append one or more elements to an Array end. Using this method, we can append multiple elements by passing multiple arguments.
Unshift method is like the push method, which works at the beginning of the array. This method is used to prepend one or more elements to the beginning of the array.
54. How are object properties assigned?
Properties are assigned to objects in the following way –
Strict mode can be enabled by adding the string literal “use strict” above the file. This can be illustrated by the given example:
56. What is the way to get the status of a CheckBox?
The status can be acquired as follows –
If the CheckBox is checked, this alert will return TRUE.
57. How can the OS of the client machine be detected?
The navigator. appVersion string can be used to detect the operating system on the client machine.
58. What is a window.onload and onDocumentReady?
The onload function is not run until all the information on the page is loaded. This leads to a substantial delay before any code is executed.
onDocumentReady loads the code just after the DOM is loaded. This allows early manipulation of the code.
The closure is a locally declared variable related to a function that stays in memory when it has returned.
60. How can a value be appended to an array?
A value can be appended to an array in the given manner –
The for-in loop is used to loop through the properties of an object.
The syntax for the for-in loop is –
In each repetition, one property from the object is associated with the variable name. The loop is continued till all the properties of the object are depleted.
A function that is declared without any named identifier is known as an anonymous function. In general, an anonymous function is inaccessible after its declaration.
63. What is the difference between .call() and .apply()?
The function .call() and .apply() are very similar in their usage except a little difference. .call() is used when the number of the function’s arguments are known to the programmer, as they have to be mentioned as arguments in the call statement. On the other hand, .apply() is used when the number is not known. The function .apply() expects the argument to be an array.
The basic difference between .call() and .apply() is in the way arguments are passed to the function. Their usage can be illustrated by the given example.
64. What is event bubbling?
*Operators are without the parenthesis.
This can be done by including the name of the required frame in the hyperlink using the ‘target’ attribute.
68. What is the role of break and continue statements?
The break statement is used to come out of the current loop. In contrast, the continue statement continues the current loop with a new recurrence.
69. Write the point of difference between a web garden and a web farm?
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. At the same time,web-farm is a larger setup that uses more than one server.
70. How are object properties assigned?
Assigning properties to objects is done in the same way as a value is assigned to a variable. For example, a form object’s action value is assigned as ‘submit’ in the following manner – Document. form.action=”submit”
DOM stands for Document Object Model and is responsible for how various objects in a document interact with each other. DOM is required for developing web pages, which includes objects like paragraphs, links, etc. These objects can be operated to include actions like add or delete. DOM is also required to add extra capabilities to a web page. On top of that, the use of API gives an advantage over other existing models.
Events are the actions that result from activities, such as clicking a link or filling a form by the user. An event handler is required to manage the proper execution of all these events. Event handlers are an extra attribute of the object. This attribute includes the event’s name and the action taken if the event takes place.
The HTML code’s parsing during page loading is paused by default until the script has not stopped executing. If the server is slow or the script is particularly heavy, then the web page is delayed.
While using Deferred, scripts delays execution of the script till the time the HTML parser is running. This reduces the loading time of web pages, and they get displayed faster.
77. What are Screen objects?
Screen objects are used to read the information from the client’s screen. The properties of screen objects are –
78. What is the unshift() method?
This method is functional at the starting of the array, unlike the push(). It adds the desired number of elements to the top of an array. For example –
The output is shown below:
79. What is unescape() and escape() functions?
The escape () function is responsible for coding a string to transfer the information from one computer to the other across a network.
The unescape() function is very important as it decodes the coded string.
It works in the following way. For example:
Output: Hello? How are you!
80. What are the decodeURI() and encodeURI()?
EncodeURl() is used to convert URL into their hex coding. And DecodeURI() is used to convert the encoded URL back to normal.
innerHTML content is refreshed every time and thus is slower. There is no scope for validation in innerHTML. Therefore, it is easier to insert rogue code in the document and make the web page unstable.
82. What does the following statement declare?
It declares a three-dimensional array.
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.
Add “ tag.
Add “//–>” without the quotes in the code just before the
Loops are useful when you repeatedly execute the same lines of code a specific number of times or as long as a specific condition is true. Suppose you want to type a ‘Hello’ message 100 times on your webpage. Of course, you will have to copy and paste the same line 100 times. Instead, if you use loops, you can complete this task in just 3 or 4 lines.
for/in a loop (explained later)
The “while loop” is executed as long as the specified condition is true. Inside the while loop, you should include the statement that will end the loop at some point in time. Otherwise, your loop will never end, and your browser may crash.
The do…while loop is very similar to the while loop. The only difference is that in do…while loop, the block of code gets executed once even before checking the condition.
The Array object has many properties and methods which help developers to handle arrays easily and efficiently. You can get the value of a property by specifying arrayname.property and the output of a method by specifying arrayname.method().
How to Create an Object
You can create an object like this:
90. What is Loop Though the Properties of an Object?
The for/in a loop is usually used to loop through the properties of an object. You can give any name for the variable, but the object’s name should be the same as an already existing object you need to loop through.
Every suite contains several tests designed to be executed for a separate module. Most importantly, they don’t conflict with any other module and run with fewer dependencies on each other (some critical situations may cause dependencies).
To avoid such issues, what you can do is;
Karma: Karma is an open-source productive testing environment. Easy workflow control running on the command line. Offers the freedom to write the tests with Jasmine, Mocha, and QUnit. You can run the test on real devices with easy debugging.
Quick Sort algorithm follows Divide and Conquer approach. It divides elements into smaller parts based on some conditions and performing the sort of operations on those divided smaller parts.
Quicksort follows the Divide-and-Conquer algorithm. It divides elements into smaller parts based on some conditions and performs the sort operations on those divided smaller parts. Hence, it works well for large datasets. So, here are the steps of how Quicksort works in simple words.
So, that is the basic outline of Quicksort. Here are the steps which need to be followed one by one to perform Quicksort.
94.How does QuickSort Work
Step 1) First, find the “pivot” element in the array.
Step 2) Start the left pointer at the first element of the array.
Step 3) Start the right pointer at the last element of the array.
Step 4) Compare the element pointing with the left pointer, and if it is less than the pivot element, then move the left pointer to the right (add 1 to the left index). Continue this until the left side element is greater than or equal to the pivot element.
Step 5) Compare the element pointing with the right pointer. If it is greater than the pivot element, move the right pointer to the left (subtract 1 to the right index). Continue this until the right-side element is less than or equal to the pivot element.
Step 6) Check if the left pointer is less than or equal to a right pointer, then saw the elements in these pointers’ locations.
Step 7) Increment the left pointer and decrement the right pointer.
Step 8) If the left pointer index is still less than the right pointer’s index, repeat the process; else, return the left pointer’s index.
So, let us see these steps with an example. Let us consider an array of elements which we need to sort is [5,3,7,6,2,9].
Here are the steps to perform Quick sort that is being shown with an example [5,3,7,6,2,9].
STEP 1) Determine pivot as a middle element. So, 7 is the pivot element.
STEP 2) Start left and right pointers as first and last elements of the array, respectively. The left pointer points to 5 at index 0, and the right pointer points to 9 at index 5.
STEP 3) Compare the left pointer element with the pivot element, since 5 < 6 shift left pointer to the right to index 1.
STEP 4) Now, still 3 <6, so shift the left pointer to one more index to the right. Now 7 > 6 stops incrementing the left pointer, and now the left pointer is index 2.
STEP 5) Now, compare the value at the right pointer with the pivot element. Since 9 > 6, move the right pointer to the left. Now, as 2 < 6, stop moving the right pointer.
STEP 6) Swap both values present at left and right pointers with each other.
STEP 7) Move both pointers one more step.
STEP 8) Since 6 = 6, move pointers to one more step and stop as the left pointer crosses the right pointer and returns the left pointer’s index.
Here, based on the above approach, we need to write code for swapping elements and partitioning the array as mentioned in the above steps.
95. How to use DOM and Events?
getElementsByTagName: To access elements and attributes using tag name. This method will return an array of all the items with the same tag name.
You plan to display the current date and time on all your web pages. Suppose you wrote the code and copied it in to all your web pages (say 100). But later, you want to change the format in which the date or time is displayed. In this case, you will have to make changes to all the 100 web pages. This will be a very time-consuming and difficult task.
Note: It is assumed that the .js file and all your web pages are in the same folder. If the external.js file is in a different folder, you need to specify your file’s full path in the src attribute.
A cookie is a piece of data stored on your computer to be accessed by your browser. You also might have enjoyed the benefits of cookies knowingly or unknowingly. Have you ever saved your Facebook password so that you do not have to type it every time you try to login? If yes, then you are using cookies. Cookies are saved as key/value pairs.
You can create cookies using document. cookie property like this.
You can even add an expiry date to your Cookie to remove the particular Cookie from the computer on the specified date. The expiry date should be set in the UTC/GMT format. If you do not set the expiry date, the cookie will be removed when the user closes the browser.
You can also set the domain and path to specify which domain and to which directories in the specific domain the Cookie belongs to. By default, a cookie belongs to the page that sets the Cookie.
//create a cookie with a domain to the current page and a path to the entire domain.
You can access the Cookie like this, which will return all the cookies saved for the current domain.
To delete a cookie, you just need to set the cookie’s value to empty and set the value of expires to a passed date.
Here, are example of simple multiplication table asking the user the number of rows and columns he wants.
100. Explain Popup Message using event with example
Display a simple message “Welcome!!!” on your demo webpage and when the user hovers over the message, a popup should be displayed with a message “Welcome to my WebPage!!!”.
Hoisting means all the declarations are moved to the top of the scope. This happens before the code is run.
For functions, this means you can call them from anywhere in the scope, even before they are defined.
For variables, hoisting is a bit different. It assigns
undefined to them at the top of the scope.
For example, calling a variable before defining it:
This may be surprising because you probably expected it to cause errors.
If you declare a function or variable, it is always moved on the top of the scope no matter where you declare it.
- What is the use of isNaN function? …
- What is negative Infinity?
- Step 1: Know what you need to study. You already know what programming language you will be using, so now you need to research what aspects of that language will be tested. …
- Step 2: Make a plan. …
- Step 3: Don’t forget the behavioral interview.