Breaking News

JAVASCRIPT: Questions and Answers (Chapter 2) (#BCANotes)(#MCANotes)(#JSTutorial)(#ipumusings)(#CSENotes)

JAVASCRIPT: Questions and Answers (Chapter 2) 

JAVASCRIPT: Questions and Answers (Chapter 2) (#BCANotes)(#MCANotes)(#JSTutorial)(#ipumusings)(#CSENotes)


Q1: What are the two basic groups of data types in JavaScript (JS)?


Answer: Two groups are:

1. Primitive types: string, number, boolean, null, undefined

2. Complex types: object (including functions)


Q2: What are the three ways to add JavaScript commands to Web Pages?


Answer: Three ways to add JavaScript commands to Web Pages are:

• Embedding code

• Inline code

• External file



Q3: What are the JavaScript types?

Answer: There are just six types in JavaScript: Object, Number, String, Boolean, Null, and Undefined.



Q4: Does JS support the following programming styles?

a. Procedural programming

b. Object Oriented programming

c. Functional programming


Answer: Yes all of these are supported.



Q5: What are global variables?

Answer: Global variables are those that are available throughout the length of the code, that is, these have 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.



Q6: How JS script can be embedded into a web page?

Answer: JavaScript can be added to a web page by embedding the code within the following script element:

<script>

 statements

</script>


Here statements are the individual lines of code. 

For example:


<script>

 window.alert("Hello World");

</script>


When the browser detects a script element, it halts the page's loading and evaluates the script's statements. The browser would then stop loading the page in order to display the "Hello World" greeting. Following the execution of the script, the browser proceeds to process the HTML file's remaining content.



Q7: Which symbol is used for comments in Javascript?


Answer

// for Single line comments

/* MultiLine

Comment */



Q8: What is an undefined value in JavaScript?

Answer

Undefined value means the:

• Variable used in the code doesn’t exist

• Variable is not assigned to any value

• Property doesn’t exist



Q9: What are the different types of errors in JavaScript?

Answer: 

There are three types of errors:

Load time errors: Errors that come up when loading a web page like improper syntax errors are known as Load time errors and it generates the errors dynamically.


Run time errors: Errors that come due to misuse of the command inside the HTML language.


Logical Errors: These are errors that occur due to the wrong logic performed on a function


Q10: What is document.write() function do?

Answer: document is an important object, and is part of the Document Object Model or Browser Object Model.

The "document" object represents the entire content of the web page. One can write new content to the web page with the write() method of the Document object.


e.g.

<script>

 document.write("<p>This paragraph is written via JS script</p>");

</script>


The document.write() function writes the content with <p> tags.

JAVASCRIPT: Questions and Answers (Chapter 2) (#BCANotes)(#MCANotes)(#JSTutorial)(#ipumusings)(#CSENotes)



Note: Although the document.write() method is a quick and easy way of writing content on your web page, it slows down the web page performance because the browser has to render again to write new content.



Q11: Show an example to include JavaScript from an external file.

Answer: To include JavaScript from an external file, the src attribute is required. The value of src is a URL link to a file containing JavaScript code, e.g.


<script src="scripfile.js"></script>



Q12: What happens if an external script is placed within the <head> element on a page?

Answer: Consider the following example:

<!DOCTYPE html>

<html>

 <head>

 <title>HTML Title</title>

 <script src="file1.js"></script>

 </head>

 <body>

 <!-- content here -->

 </body>

</html>



When the external file is placed in the <head> section, the browser downloads the JS file first, parses it and interprets it before rendering the HTML page. Too many JS files in the <head> section or a big JS file can bring noticeable delay in rendering the HTML page. 


Another better approach is to place JS files in body tags where required or load deferred or asynchronous scripts.



Q13: What are deferred scripts?

Answer: The defer attribute on an <script> element informs the browser that the download should begin immediately but the execution is deferred. Execution is done once the page is parsed.

e.g.


<!DOCTYPE html>

<html>

 <head>

 <title>Title Page</title>

 <script defer src="file1.js"></script>

 </head>

 <body>

 <!-- content here -->

 </body>

</html>



Q14: What are asynchronous scripts?

Answer: When the async attribute is used, the script is downloaded in parallel to parsing the page, and executed as soon as it is available (even before parsing completes).


For example:

<!DOCTYPE html>

<html>

 <head>

 <title>Page Title</title>

 <script async src="file1.js"></script>

 <script async src="file2.js"></script>

 </head>

 <body>

 <!-- content here -->

 </body>

</html> 


In the above example, if "file2.js" gets downloaded before "file1.js", the "file2.js" will be executed first.


👉See Also:

Javascript: An Introduction (Q & A) Chapter 1