JavaScript Concepts
Error handling, “try..catch”
As a programmer, we made a mistake in our program, no matter how much better we are. sometimes our scripts have errors. They may occur because of our mistakes, an unexpected user input, an erroneous server response, and for a thousand other reasons. After getting an error our script going die but there is an option to handle it that is try…catch statement.
The JavaScript statements try
and catch
come in pairs:
try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}
try…catch statement with no error
try {
alert(‘No Errors here’);
} catch(err) {
alert(‘Catch is ignored, because there are no errors’);
}
output: No Errors here
try…catch statement with error
try {
numbers; // numbers variable is not defined
} catch(err) {
alert(‘Numbers in not defined’);
}
output: Number is not defined
Comments
comments can be single-line: starting with //
and multiline: /* ... */
. JavaScript comments can be used to explain JavaScript code, and to make it more readable. JavaScript comments can also be used to prevent execution when testing an alternative code. The commented line will not execute.
Single line comment
var x = 5; // Declare x, give it the value of 5
Multiline comment
/*
The code below will change
the heading with id = “myHeading”
and the paragraph with id = “myContent”
on my web page:
*/
document.getElementById(“myHeading”).innerHTML = “My First Page”;
document.getElementById(“myContent”).innerHTML = “My first paragraph.”;
JavaScript Functions
A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when “something” invokes it (calls it). Functions are one of the fundamental building blocks in JavaScript.
Example:
function myFunction(a, b) {
return a + b; // The function returns the product of p1 and p2
}
Function with Default Parameter Value
The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined. In this case, the default value that we specify is applied by the compiler.
Example:
function multiply(a, b = 1) {
return a * b;
}console.log(multiply(5, 2));
// expected output: 10console.log(multiply(5));
// expected output: 5
Anonymous function
An anonymous function is a function without a name. An anonymous function is often not accessible after its initial creation.
Example:
let show = function () { console.log(‘Anonymous function’); };
The anonymous function has no name between the function
keyword and parentheses ()
. Because we need to call the anonymous function later, we assign the function to the show
variable.
Arrow Function
Arrow functions were introduced in ES6. Arrow functions allow us to write shorter function syntax
Before it was like this-
let arrowFunction = function myFunction {
return “This is function”
}
After arrow function-
let arrowFunction= () => {
return “This is function”;
}
If the function returns just one statement you can remove brackets from the function declaration
let arrowFunction = () => This is function