JavaScript Notes: Functions| PDF | B.Tech | B.E
top of page

JavaScript Class Notes: Functions

Updated: Oct 16, 2022

Mobiprep has created last-minute notes for all topics of JavaScript to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on JavaScript.

JavaScript Class Notes: Functions

Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.

JavaScript Notes: Functions



Question 1. Define JavaScript Functions.

The JavaScript functions are blocks of code which are meant to perform a particular task. Functions are written for code reusability. When a specific task has to be performed many times, the functions are used. Once the functions are defined, they can be used many times. The block of code within a function is executed only when the function is called. After the function is executed, the function returns some values to the calling statement. These are called return values.

 

Question 2. Explain the JavaScript Function Syntax.

In JavaScript, functions are defined using the ‘function’ keyword. The syntax of the function definition is given below:


function name(parameter1, parameter2, parameter3) {
  // code to be executed
}

The names within ( ) are called parameters. The values passed to the parameters are called arguments.

The function is executed only when it is called or invoked. After the function is executed, the function returns some values to the caller. These are called return values.

Example:


var x = myFunction(77, 36);   // Function is called, return value will end up in x
function myFunction(a, b) {
  return a * b;             // Function returns the product of a and b
}

 

Question 3. What are the types of arguments that can be passed to JavaScript functions?

The following are the types of arguments that can be passed to a JavaScript Function:

a. Default Argument

The default arguments are those which are initialized with default values. The default values are use when the value of the argument is not passed.

Syntax


function Name(paramet1 = value1, paramet2 = value2 .. .) {
    // statements
}

Example

		function Hello(num1, num2 = 5) { 
           		 return num1 * num2; 
        		}  	
        		document.getElementById("RootWorkz").innerHTML 
                	= Hello(4);

In the above example, the function call Hello(4) passes only one argument to the function. As the value of num2 is not passed, the default value 2 is used by the function.

b. Arguments Object

The arguments object is an inbuilt object which contains all the argument values passed to the function.

Example:

function Hello() {
var i;
var maxnum = -Infinity;
for(i = 0; i < arguments.length; i++) {
if (arguments[i] > maxnum) {
maxnum = arguments[i];
}
}
return maxnum;
}
document.getElementById("geeks").innerHTML
= Hello(134, 200, 205, 400, 450)

c. Argument Passed by Value

In pass by value, values of the variables are passed to the arguments instead of address.

Example:

function Hello(var1, var2) {  
            document.write("Inside the function");  
            var1 = 11;  
            var2 = 20;
            document.write("var1 =" + var1 +" var2 =" +var2);  
        }  
Hello(4,5);

In the above example, the values 4 and 5 are passed to the arguments var1, and var2. Hence, the arguments are passed by value.


d. Objects Passed by Reference

In pass by reference, address (or reference) of the variable is passed to the function arguments, instead of values. Here, the object references are passed to the functions.

Example:

function Hello(varObj) {  
            document.write("Inside the function");  
            varObj.a = 100;  
            document.write(varObj.a); 
        } 
        // Create object 
        varObj = {a:1};           
        /* Function calling */ 
        RootworkzvarObj)  

 

Question 4. What are functional components?

Functional components are JavaScript functions that take props as input arguments and return React elements. Functional components are reusable blocks of code. The functional components are used for developing a user interface. They accept data and display it in another form.

Example:

function Hello(props){
return <div>Hello {props.name}</div>
}

The above functional component accepts one ‘props’ as an argument and returns a React element. ‘props’ are nothing but components properties.

The functional components lack state and lifecycle methods. So, they are called stateless components.

The functional components make the code more readable and easy to debug.

 

Question 5. Define JavaScript Events.

Events are used to trigger JavaScript code. JavaScript interacts with HTML through events. The events are used to execute the JavaScript code to perform a specific task in the web page.

Whenever events are detected, the JavaScript code is executed. The syntax of an event is given below:


<element event='some JavaScript'>

Examples of events are

  • Clicking a button in the webpage

  • resizing the window etc.,

Example: Clicking a button


<button onclick="document.getElementById('demo').innerHTML = Date()">The time is?</button>
 

Question 6.What are the ways in which data can be displayed by JavaScript?

The data can be displayed in the following ways:

1. Using innerHTML

The HTML content is defined by the innerHTML property. JavaScript accesses the HTML element using the ‘id’ attribute of the element.

Example:

<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>

2. Using document.write()

The content can be displayed using the document.write() function. It is the commonly used method of displaying data.

Example:

document.write(4 + 8);

3. Using window.alert()

Window.alert() opens an alert box to display the data.

Example:

window.alert(4 + 8);

4. Using console.log()

The console.log() method is used to display data in the web browser.

Example:

console.log(4 + 8);

 

Question 7. Explain the role of 'window' keyword in JavaScript.

The window object is used to represent the window of the web browser. It is used to control the various operations performed in the browser window. The ‘window’ keyword is used to access the current code running in the browser. The global variables and the global functions are the properties and methods of the window object respectively.

The window object is used to open, close and resize the browser window. All the document objects are the properties of the window object.

 

Question 8. Explain blur() function in JavaScript.

The blur() function is used to un-focus an element (or remove focus from an element).

Syntax:

HTMLElementObject.blur()

Example:

document.getElementById("myText").blur();

The above statement is used to remove focus from the Text field in HTML.

 

Question 9. What are anonymous functions in JavaScript?

Functions without names are called anonymous functions. These functions are stored in variables. These functions are called using the variable name.

Example:
	var x = function (a, b) {return a * b};
var z = x(4, 3);

Here, the function is assigned to the variable ‘x’. Hence, the function is called using the variable name ‘x’.

 

Question 10. Write a note on Callback function.

The functions passed as arguments to other functions are called callback functions. The callback functions are used to make the functions execute sequentially. The callback functions help us to execute a function only after the specific task is completed.

Example:

function greeting(name) {
  alert('Hello ' + name);
}
function process (callback) {
  var name = prompt('Please enter your name.');
  callback(name);
}
process (greeting);

Here, the function ‘greeting’ is passed as an argument to the function ‘process’. Hence, the ‘process’ function is an example of callback function.

 

Question 11. Define Closures in JavaScript.

The JavaScript variables can be either local or global (variable scope). A local variable can be used only within a function. A global variable can be used by all the functions in the page. The closures are used to make the global functions local.

Example:

// Initiate counter
var counter = 0
// Function to increment counter
function add() {
  counter += 1;
}
// Call add() 3 times
add();
add();
add();
In the above example, the ‘counter’ variable is a global variable.  If the value of the ‘counter’ variable should not be changed by other functions, we have to make it a local variable which belongs the ‘add’ function.

So, we use closures to make the ‘counter’ variable ‘local’.

var add = (function () {
  var counter = 0;
  return function () {counter += 1; return counter}
})();
add();
add();
add();

Here, the return value of the self-invoking anonymous function is assigned to the ‘add’ variable. As the anonymous function is assigned to a variable, it is called using the variable name. Note that the ‘counter’ variable can be used only within the ‘add’ function.

Hence, closure functions makes the functions to have ‘private’ or ‘local’ variables.



Top 40 Javascript Interview Questions and Answers
.pdf
Download PDF • 1.15MB


 

Mobiprep is a one stop solution for all exam preperation needs. Get all exam prep resources at a single platform.
Free Practice Tests
Top Placement Interview Questions
Top Placement MCQ's Questions
 


152 views0 comments

Recent Posts

See All
bottom of page