Learn 10 Basic JavaScript Fundamental Concepts as a Junior Developer.
Objects and Functions
Objects and functions are special because I can manipulate them from my
code. For example, I can connect them to other values.
console.log({});
console.log([]);
console.log(x => x * 2);
“try…catch” syntax
No matter how great we are at programming, sometimes our scripts have errors. They may occur because of our mistakes, unexpected user input, an erroneous server response, and for a thousand other reasons.
try-catch construct has two main blocks: try and then catch:
try { // code… }
catch (err) { // error handling }
It works like this:
- First, the code in try{…}is executed.
- If there were no errors, then catch (err)is ignored: the execution reaches the end of
try
and goes on, skippingcatch
. - If an error occurs, then the
try
execution is stopped, and control flows to the beginning ofcatch (err)
. Theerr
variable (we can use any name for it) will contain an error object with details about what happened.
Do You know try…catch only works for runtime errors?
A program during execution is called runtime errors. The main purpose of try…catch to work, the code must be runnable.
try {
{{{{{
} catch (err) {
alert(“Error has occured”);
}
try…catch works synchronously
If a programmer used setTimeOut method, then try catch won’t catch it.
try {
setTimeout(function() {
myVariable
}, 1100);
} catch (err) {
alert( “didn't work” );
}
But, if you want to catch an exception inside a function try…catch must be used.
setTimeout(function() {
try {
myVariable;
} catch {
alert( “Error” );
}
}, 1000);
Error Object
When an error occurs, JavaScript generates an object that are passed as an argument to catch.
try {
// code
} catch (err) { //error object
// code
}
try…catch”
Let’s go to a real life example, JSON. We mainly used to get the decoded data by using JSON parsing. A simple example of JSON is:
json = ‘{“name”:”Mahmudur”, “age”: 23, “occupation”: “Web Developer”}’;
If the json is not correct, it generates an error.
let json = “{ wrong json }”;
try {
let user = JSON.parse(json);
alert( user.age ); // doesn’t work
} catch (err) {
alert( err.name );
alert( err.message );
}
Own errors
Sometimes, you see that json is syntactically correct but you don’t have a occupation property. See this.
let json = ‘{ “age”: 23 }’;try {
let user = JSON.parse(json); // ← no errors
alert( user.occupation );
} catch (err) {
alert( “no execution” );
}
instanceof operator
We can check the type of error by using instanceof operator.
try {
varA = { /*…*/ };
} catch (err) {
if (err instanceof ReferenceError) {
alert(‘Error has occured’);
}
}
Reference error mainly used for undefined variable.
try…catch…finally
finally statement used to execute the code after try and catch. The code is:
try {
// code
} catch (err) {
// code
} finally {
// code
}
Single Line Comments
Single line comments start with //.
Any text between // and the end of the line will be ignored by JavaScript .
Example
// Change heading:
document.getElementById(“myH”).innerHTML = “My First Page”;
// Change paragraph:
document.getElementById(“myP”).innerHTML = “My first paragraph.”;
Multi-line Comments
Multi-line comments start with /* and end with */.
Any text between /* and */ will be ignored by JavaScript.
This example uses a multi-line comment (a comment block) to explain the code:
Example
/*
The code below will change
the heading with id = “myH”
and the paragraph with id = “myP”
in my web page:
*/
document.getElementById(“myH”).innerHTML = “My First Page”;
document.getElementById(“myP”).innerHTML = “My first paragraph.”;
Introduction to cross browser testing
As a web developer, it is your responsibility to make sure that not only do your projects work, but they work for all your users, no matter what browser, device, or additional assistive tools they are using.
Client And Server Caching in Web Application Development
Client Caching
Server Caching
Hybrid Caching
Local Caching
thank you for reading this article.😉