In: Computer Science
1.JavaScript is a dynamic and loosely typed language. What is the significance of these language descriptors?
2.JavaScript variables are hoisted at run time. What does this mean and how does it impact function design?
3.Define a variable in JavaScript that is assigned an anonymous function. This function should take two values as input and give back the sum
4. Most of the JavaScript use in Web Development is through the DOM. What does DOM stand for?
5.Two of the primary objects in the DOM are $window and $document. What do they represent and what sort of function does each control?
6.The DOM uses a node system. Briefly describe how this node system pertains to controlling / interacting with the HTML page
The answer to the above question can be explained as follows-
Question 1- Answer : JavaScript is a dynamic typed language which means checking the types of variables while running the program. So this concept deals with when to check for the datatypes. And JavaScript is also loosely typed language which is another way to say that type checking in JS is very relaxed. We do not need to defined the variables, just declare the variable and not explicitly tell the datatype. So in loosely typed, values are converted from one type to another implicitly. These two features make the program language easier to read and write but may lead to more errors.
Question 2- JavaScript variables are hoisted at run time means that when the variables are declared , the variable declarations are hoisted at the top locally/globally, whichever scope they are declared in. That means that the declarations are move to the top of the scope. We need to understand that all the declarations of var, let, const, function and class are all hoisted to the top of scope. But this concept should not be comprehended in a literal sense, rather we can say that JS compiler reads these declarations first to allot them memory space.
Eg- when we write code-
console.log(data)
data = "1234"
This code does not give error message that variable data is not defined. But it will output undefined. This is because only the declarations are moved to the top and not the actual values.
Similarly for function, the function declarations are hoisted at the top too, and these are above the variable declarations.
So while designing a function, we ourselves make sure that the variables and functions are at least declared at the top of the script.
Question 3- Below is the code for anonymous function named sum which takes in two parameters and then returns the sum of these numbers.
Anonymous sum function-
var sum = function( x, y){
var z = x+y;
return z;
}
console.log("Sum from anonymous function =
",sum(4,5));
Image of Code snippet with output is given below-
Question 4- DOM stands for Document object model.It is basically an object model as clear from the full form which acts as an interface to access and modify the elements in an HTML document.
Question 5- $window and $document are two primary objects of the document object model.
Window object is the entire screen that is visible in a browser,so its more like a window in any browser which gets loaded at the beginning and then the other objects gets loaded. It controls a variety of functions like opening a new browser window, closing the current window, displaying any alert box in window, stopping the current window from getting loaded etc.
Where as Document is the object that gets loaded after the window like HTML or other web pages and this document gets loaded inside a window object. HTML is the document object. It controls the functions like URL, title, event listeners, getElementById, getElementByClassName() and other such functions.
Question 6- The Document object Model uses the node system. That means the HTML page looks like a tree or forest with all elements being treated as a node and to interact with the node, we need function of DOM.
An example of node system is like this-
In this node system , we find elements or nodes of HTML document with methods like getElementById(), getElementByClassName() and other functions and use this to change or modify the elements of HTML.
If this answer helps, please give an up vote and feel free to comment for any query.