Functions
- What is a function?
- Pre-defined functions
- Scope of Variables
- Function Expressions
- Anonymous Functions
- Callback Functions
Objects
- What is an object?
- Accessing Object Properties
- Calling an Object’s Methods
- Altering Properties/Methods
Array Properties and Methods
- The length property
- Stack Methods
- Queue Methods
- Reordering Methods
- Manipulation Methods
- Location Methods
- Iterative Methods
- Working with array of objects
DOM
- The Document Object Model
- Node Relationships
- Working with the Contents of Elements
- Getting Elements by ID, Tag Name, or Class
- Adding and removing element
Built-in Objects
Using Events in JavaScript
Handling Input and Output
Ajax and JSON
This is an important concept that we’ll need later on—functions in JavaScript are actually data. This means that the following two ways to define a function are exactly the same:
function f(){return 1;}
var f = function(){return 1;}
The second way of defining a function is known as function literal notation. When you use the typeof operator on a variable that holds a function value, it returns the string “function”.
function f(){return 1;}
typeof f
//"function"
So JavaScript functions are data, but a special kind of data with two important features:
- They contain code
- They are executable (can be invoked)
var sum = function(a, b) {return a + b;}
var add = sum;
delete sum
//true
typeof sum;
//"undefined"
typeof add;
//"function"
add(1, 2);
//3
As you saw before, the way to execute a function is by adding parentheses after its name. As the next example demonstrates, this works regardless of how the function was defined. In the example, you can also see how a function is treated as a normal variable—it can be copied to a different variable and even deleted.
Because functions are data assigned to variables, the same rules for naming functions apply as for naming variables—a function name cannot start with a number and it can contain any combination of letters, numbers, and the underscore character.
Quiz
1. Choose the correct code to be inserted in order to invoke the function.
var fun=function lfc(x){
console.log(x*x);
}
A. thisfun(10)
B. lfc(10)
C. function thisfun(10)
D. None of the above
2. How many arguments does function foo accept?
const foo = function(a, b){
return a+b;
}
A. 0
B. 1
C. 2
D. 3
3. What will be the output of the below code?
var changeVal = function(p){
return p+2;
}
console.log(changeVal(2));
A. 4
B. 22
C. Error: changeVal is not a function
D. undefined
4. What is the output of this program?
f91();
var f91 = function () {
console.log(‘f91’);
};
A. f91
B. Error
C. undefined
D. None of these