How to works your JavaScript Panther & Handle Error
How to works your JavaScript Panther & Handle Error
Before we start we learn about Javascript as a Girlfriend[Part One]
It’s [PartTWO] What does your girlfriend behave ;) ;
Your girlfriend JavaScript is a single-threaded programming language, which means it has a single Call Stack. Therefore it can do one thing at a time.
Don’t need to worry about the single call stack
Think She has 5 friends who want to get a relationship with JS
Then She will make One Queue Of Boys like this
[Line-1,Line-2,Line-3,Line-4,Line-5];
Present time don’t think she is your girlfriend think she is your crush and your
position in
LINE-5 :) So what’s gonna happen here?
Then She will give the first One as a first chance ;) when she is complete with 4 boys to get a relationship then she will join with you. That’s funny, right? She is not multitasking like other girls dude. She is very loyal :)
How to handle your Girlfriend JS angriness?
There is One tip: when your girlfriend is angry with you she will give you an error. It’s normal but you have to handle it because she is your girlfriend dude. I’m gonna share here some tips on how to handle her error.
No matter how great we are at loving her, sometimes our code has errors. They may occur because of your mistakes, unexpected things, an erroneous server response, and for a thousand other reasons.
You easily manage error and control her angriness by try Catch Block
try {
//You: Do some things for me…
//She: Okey Dear
} catch (err) {
// She: No Lover I Don’t wanna do anything for you
}
Imagine you said to her do something for me if her mood is okay and you did not make any mistake with her she will say Okey Dear but if she gets any mistake of yours she will go to the err block and she will say no lover I don’t wanna do anything for you have you understand?
Okay, I’m giving you an example:
try {
alert(‘Dear JS I Love you bae’); // She will respond to it
Hay Mouna Bae; // error, She will Angry Because you are cheating with her
alert(‘She is my just friend’); // (This line will not execute because she minded and she will go to the error block means she is an angry dude)
} catch (err) {
alert(`No You don’t love me you cheat with me you talk with mouna`); // Looks she is sensitive :)
}
Her angriness has extra things I’m gonna share with you guys
try {
You talking with other girls; // error, variable is not defined!
} catch (err) {
alert(err.name); // ReferenceError
alert(err.message); // You talking with other girls is not defined
alert(err.stack); // ReferenceError: You talking with other girls is not defined at (…call stack)
// Can also show an error as a whole
// The error is converted to string as “name: message”
alert(err); // ReferenceError: You talking with other girlsis not defined
}
Look She can say why she is angry with you. What you have to do to remove her anger is cute right? She is a nice girl who is saying how to remove her anger. Love her :)
Optional “catch” binding
Usually it’s used to decode data received over the network, from the server or another source.
We receive it and call JSON.parse like this:
let you= ‘{“name”:”Bae”, “age”: 30}’; // data from the server
let user = JSON.parse(json); // convert the text representation to JS object
// now user is an object with properties from the string
alert( user.name ); // Bae
alert( user.age ); // 30
It will Work because it’s you she know who you are you are loveable person of JS but without you she will response like this :
let json = “{ Other Person }”;
try {
let user = JSON.parse(json); // ← when an error occurs…
alert( user.name ); // doesn’t work
} catch (err) {
// …the execution jumps here
alert( “You are not the right person you don’t love me that’s why you code like this :) get lost” );
alert( err.name );
alert( err.message );
}
Nice huhhhhh. She knows you because she knows that your code is good for her that’s why she didn’t give you an error but when other people try to enter her life she gives him an error.
Let’s talk about — Throwing Our Own Error
Throw Operator
let lover= ‘{ “age”: 30 }’; // incomplete data
try {
let user = JSON.parse(lover); // ← no errors
if (!user.name) {
throw new SyntaxError(“Incomplete data: no name”); // (*)
}
alert( user.name );
} catch (err) {
alert( “JSON Error: “ + err.message ); // JSON Error: Incomplete data: no name
}
SyntaxError
In the line (*), the throw operator generates a SyntaxError with the given message, the same way as JavaScript would generate it itself. The execution of try immediately stops and the control flow jumps into the catch.
Now catch became a single place for all error handling: both for JSON.parse and other cases.
There are other things to do to control the anger of your girlfriend it’s Called
Try Catch Finally
try {
… try to execute the code …
} catch (err) {
… handle errors …
} finally {
… execute always …
}
After Completed her angriness you have to know how to style her:
First of all, you have to learn how to style your code. When your code style will be good or better your lover will be happy with you and she will not be angry with you ever ;)
Coding Style :
Our code must be as clean and easy to read as possible.
That is actually the art of programming — to take a complex task and code it in a way that is both correct and human-readable. A good code style greatly assists in that.
Bad Style : if (str < 0) {alert(`I ${str} you so much`);}
Good Style :
if (n < 0) {
alert(`I ${str} you so much`);
}
Best Rules of Install ESLint with the command npm install -g eslint Then you can style your Code easily
Comments :
It’s Good to remember your Lover What you are doing in Code then you have to comment on remembering code and some hard things :)
An important sign of a good developer comment: their presence and even their absence.
Single Line Comment: //Hello Js I Love you, Bae
Multiline Comment :
/**
I Love you 3000
- */
Cross Browser Testing
This thing will help you to know Your girlfriend JS Cross-Browser things Let’s start
what is cross-browser testing?", "what are the most common types of problems you'll encounter?", and "what are the main approaches for testing, identifying, and fixing problems?
Cross-Browser testing is testing when you will test your code on another platform
Suppose you are comfortable with Chrome and you didn't test your code in opera and other platforms like Firefox, Safari, Chrome, or IE/Edge.and when you are trying to run your project here your code gets an error and what will you do?
You have to select your page where the problem starts and develop this section or page. That’s types of gonna happen big project like e-commerce project
Testing/Discovery:
- Test it in a couple of stable browsers on your systems, like Firefox, Safari, Chrome, or IE/Edge.
- Do some lo-fi accessibility testing, such as trying to use your site with only the keyboard, or using your site via a screen reader to see if it is navigable.
- Test on a mobile platform, such as Android or iOS.
At this point, fix any problems you find with your new code.
Testing on prerelease browsers
- Firefox Developer Edition
- Edge Insider Preview
- Safari Technology Preview
- Chrome Canary
- Opera Developer
- Continue Day by Day