Javascript Deck 2 Flashcards

1
Q

What are all the looping structures in JavaScript ?

A

while loop: A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.
for loop: A for loop provides a concise way of writing the loop structure. Unlike a while loop, for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping.
do while: A do-while loop is similar to while loop with the only difference that it checks the condition after executing the statements, and therefore is an example of Exit Control Loop.

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

How can the style/class of an element be changed?

A

To change the style/class of an element there are two possible ways. We use document.getElementByID method

document.getElementById(“myText”).style.fontSize = “16px;
document.getElementById(“myText”).className = “class”;

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

Explain how to read and write a file using JavaScript?

A

The readFile() functions is used for reading operation.
readFile( Path, Options, Callback)
The writeFile() functions is used for writing operation.
writeFile( Path, Data, Callback)

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

What is called Variable typing in JavaScript ?

A

The variable typing is the type of variable used to store a number and using that same variable to assign a “string”.

Geeks = 42;
Geeks = “GeeksforGeeks”;

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

How to convert the string of any base to integer in JavaScript?

A

In JavaScript, parseInt() function is used to convert the string to an integer. This function returns an integer of base which is specified in second argument of parseInt() function. The parseInt() function returns Nan (not a number) when the string doesn’t contain number.

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

Explain how to detect the operating system on the client machine?

A

To detect the operating system on the client machine, one can simply use navigator.appVersion or navigator.userAgent property. The Navigator appVersion property is a read-only property and it returns the string that represents the version information of the browser.

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

What are the types of Pop up boxes available in JavaScript?

A

There are three types of pop boxes available in JavaScript.

Alert
Confirm
Prompt

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

What is the difference between an alert box and a confirmation box?

A

An alert box will display only one button which is the OK button. It is used to inform the user about the agreement has to agree. But a Confirmation box displays two buttons OK and cancel, where the user can decide to agree or not.

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

What is the disadvantage of using innerHTML in JavaScript

A

There are lots of disadvantages of using the innerHTML in JavaScript as the content will replace everywhere. If you use += like “innerHTML = innerHTML + ‘html’” still the old content is replaced by HTML. It preserves event handlers attached to any DOM elements.

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

What is the use of void(0) ?

A

The void(0) is used to call another method without refreshing the page during the calling time parameter “zero” will be passed.

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

What are JavaScript Cookies ?

A

Cookies are small files that are stored on a user’s computer. They are used to hold a modest amount of data specific to a particular client and website and can be accessed either by the web server or by the client’s computer. When cookies were invented, they were basically little documents containing information about you and your preferences. For instance, when you select the language in which you want to view your website, the website would save the information in a document called a cookie on your computer, and the next time when you visit the website, it would be able to read a cookie saved earlier.

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

How to read a cookie using JavaScript?

A

The value of the document.cookie is used to create a cookie. Whenever you want to access the cookie you can use the string. The document.cookie string keep a list of name = value pairs separated by semicolons, where name is the name of a cookie and the value is its string value.

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

How to create a cookie using JavaScript?

A

To create a cookie by using JavaScript you just need to assign a string value to the document.cookie object.

document.cookie = “key1 = value1; key2 = value2; expires = date”;

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

How to delete a cookie using JavaScript?

A

Deleting a cookie is much easier than creating or reading a cookie, you just need to set the expires = “past time” and make sure one thing defines the right cookie path unless few will not allow you to delete the cookie.

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

What are escape characters and escape() function?

A

Escape character: This character is required when you want to work with some special characters like single and double quotes, apostrophes, and ampersands. All the special character plays an important role in JavaScript, to ignore that or to print that special character, you can use the escape character backslash “\”. It will normally ignore and behave like a normal character.

// Need escape character
document.write(“GeeksforGeeks: A Computer Science Portal “for Geeks” “)
document.write(“GeeksforGeeks: A Computer Science Portal "for Geeks" “)

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

Whether JavaScript has a concept-level scope?

A

JavaScript is not concept-level scope, the variables declared inside any function have scope inside the function.

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

How generic objects can be created in JavaScript?

A

To create a generic object in JavaScript use:

var I = new object();

18
Q

Which keywords are used to handle exceptions?

A

When executing JavaScript code, errors will almost definitely occur. These errors can occur due to the fault from the programmer’s side due to the wrong input or even if there is a problem with the logic of the program. But all errors can be solved by using the below commands.

The try statement lets you test a block of code to check for errors.
The catch statement lets you handle the error if any are present.
The throw statement lets you make your own errors.

19
Q

What is the use of the blur function?

A

It is used to remove focus from the selected element. This method starts the blur event or it can be attached to a function to run when a blur event occurs.

20
Q

What is the unshift method in JavaScript?

A

It is used to insert elements in the front of an array. It is like a push method that inserts elements at the beginning of the array.

21
Q

What is the ‘Strict’ mode in JavaScript and how can it be enabled?

A

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program or a function in a “strict” operating context. This strict context prevents certain actions from being taken and throws more exceptions. The statement “use strict” instructs the browser to use the Strict mode, which is a reduced and safer feature set of JavaScript.

22
Q

How to get the status of a CheckBox?

A

The DOM Input Checkbox Property is used to set or return the checked status of a checkbox field. This property is used to reflect the HTML Checked attribute.

document.getElementById(“GFG”).checked;
If the CheckBox is checked then it returns True.

23
Q

How to explain closures in JavaScript and when to use it?

A

The closure is created when a child functions to keep the environment of the parent’s scope even after the parent’s function has already executed. The Closure is a locally declared variable related to a function. The closure will provide better control over the code when using them.

// Explanation of closure
function foo() {
let b = 1;
function inner() {
return b;
}
return inner;
}
let get_func_inner = foo();

console.log(get_func_inner());
console.log(get_func_inner());
console.log(get_func_inner());

24
Q

What is the difference between call() and apply() methods ?

A

Both methods are used in a different situation

call() Method: It calls the method, taking the owner object as argument. The keyword this refers to the ‘owner’ of the function or the object it belongs to. We can call a method that can be used on different objects.
apply() Method: The apply() method is used to write methods, which can be used on different objects. It is different from the function call() because it takes arguments as an array.

25
Q

How to target a particular frame from a hyperlink in JavaScript ?

A

This can be done by using the target attribute in the hyperlink. Like

<a>New Page</a>

26
Q

Write the errors shown in JavaScript?

A

There are three different types of errors in JavaScript.

Syntax error: A syntax error is an error in the syntax of a sequence of characters or tokens that are intended to be written in a particular programming language.
Logical error: It is the most difficult error to be traced as it is the error on the logical part of the coding or logical error is a bug in a program that causes to operate incorrectly and terminate abnormally.
Runtime Error: A runtime error is an error that occurs during the running of the program, also known as an exception.

27
Q

What is the difference between JavaScript and Jscript?

A

JavaScript

It is a scripting language developed by Netscape.
It is used to design client and server-side applications.
It is completely independent of Java language.
Jscript

It is a scripting language developed by Microsoft.
It is used to design active online content for the word wide Web.

28
Q

What does var myArray = [[]]; statement declares?

A

In JavaScript, this statement is used to declare a two-dimensional array.

29
Q

How many ways an HTML element can be accessed in JavaScript code?

A

There are four possible ways to access HTML elements in JavaScript which are:

getElementById() Method: It is used to get the element by its id name.
getElementsByClass() Method: It is used to get all the elements that have the given classname.
getElementsByTagName() Method: It is used to get all the elements that have the given tag name.
querySelector() Method: This function takes CSS style selector and returns the first selected element.

30
Q

What is the difference between innerHTML & innerText?

A

The innerText property sets or returns the text content as plain text of the specified node, and all its descendants whereas the innerHTML property sets or returns the plain text or HTML contents in the elements. Unlike innerText, inner HTML lets you work with HTML rich text and doesn’t automatically encode and decode text.

31
Q

What is an event bubbling in JavaScript?

A

Consider a situation an element is present inside another element and both of them handle an event. When an event occurs in bubbling, the innermost element handles the event first, then the outer, and so on.

32
Q

What will be the output of the following code?

A

let X = { geeks: 1 };
let Output = (function () {
delete X.geeks;
return X.geeks;
})();

console.log(output);

Here the delete will delete the property of the object. X is the object with the geek’s property and it is a self-invoking function that will delete the geek’s property from object X so the result will be undefined.

33
Q

How are JavaScript and ECMA Script related?

A

JavaScript is the main language that has to maintain some rules and regulations which is ECMA Script, these rules also bring new features for the language JavaScript.

34
Q

How to hide JavaScript code from old browsers that don’t support JavaScript?

A

To hide the JavaScript codes from the old browsers that don’t support JavaScript you can use

<!-- before <script> tag and another //-->

after </script> tag

all the old browsers that will take that as a long comment of HTML. New browsers that support JavaScript will take that as an online comment

35
Q

What will be the output of the following code?

A

let output = (function(x) {
delete x;
return x;
})(0);

document.write(output);

The output will be 0. The delete operator is used to delete the operator of the object but the X is not the object here it is a local variable. The delete operator doesn’t affect local variables.

36
Q

In JavaScript, answer if the following expressions result in true or false.

A

“0” == 0 // true or false ?
“” == 0 // true or false ?
“” == “0” // true or false ?
The result will be True for 1st and 2nd case and False for the 3rd case.

37
Q

How to use any browser for debugging?

A

By pressing the F12 we can trigger the debugging mode of any browser and can view the result by taping the console.

38
Q

What is javascript Hoisting?

A

When any interpreter runs the code then all the variables are re-hoisted to the top of the original scope. This method is applicable for declaration not for the initialization of a variable. This is known as a javascript Hoisting.

39
Q

What is the syntax of ‘Self Invoking Function’ ?

A

The syntax for Self-Invoking Function: The last bracket contains the function expression.

(function () {
return // body of the function
}());

40
Q

How to use external JavaScript file in another JavaScript file?
You can

A

NOR NECESSARY CAN JUST USE JS MODULES
You can use the below code to use external JavaScript code in another JavaScript file.

javascript
let script = document.createElement(‘script’);
script.src = “external javascript file”;
document.head.appendChild(script)