Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
I’m going to have a party when I run out of snacks, so I ask a friend to buy some snacks for me.
Hey, can you go to the store and get some snacks, please? Then she said what kind of snacks to buy? And then you say, you know, potato chips or something. We don’t have any snacks now. She said you want me to buy some bags of chips? And then you start laughing. Whatever, about five bags. Buy what chips you want and say, “Oh, no, I’ll go into detail.” Then explain exactly what you want and how many chips you need. “Go buy five medium bags of bacon-flavored potato chips.” Ten minutes later, she came back empty-handed and said, “They don’t have medium size bags of potato chips.”
Most people have a stereotype of “people who work in software” because they demand accuracy to the point of being ridiculous.
Of course, the reality is more complicated than that. There are no two types of people. And for some people, computer programming is better for them because computers are absolutely accurate and incredibly precise. That doesn’t mean you can’t be a good developer if you don’t think of yourself as a “math person.” You can be a good computer practitioner if you actively understand how computers work.
SyntaxError
console,log("hello world")
Copy the code
This behavior of the computer can lead to many errors. If you type “console comma log” instead of “console dot log”, JavaScript will say “I don’t know what you mean”. You make mistakes, your program makes mistakes, that’s life. Every programmer makes mistakes. It doesn’t matter. What matters is how you handle them. Handling errors is a basic skill. This makes programming different from most other jobs: errors are guaranteed, you can’t completely avoid them, and dealing with them is part of your job.
Here we have a function definition, and at the end, we have a parenthesis. The parentheses shouldn’t be there, breaking the program, so JavaScript prompts: “SyntaxError: Unexpected Token)”. That parenthesis was accidental.
const cube = (num) = > {return num * num * num})
Copy the code
Here we have a function definition, and at the end, we have a parenthesis. Should not appear there and break the program, so JavaScript says: “SyntaxError: unexpected tag) “. That parenthesis was accidental.
ReferenceError
Syntaxerrors are like gibberish. No one understands. The next error is similar to syntax, but instead of breaking the syntax of the language, it breaks the syntax of your own code.
const abs = (num) = > {
if(num > 0) {return num;
}else if(num < 0) {return -num;
}else{
return 0; }}Copy the code
Creates the abs function that returns the absolute value of a number. If you call ADS instead of ABS, the JavaScript interpreter will complain. ReferenceError: ADS is not defined. Because of a typo, you end up calling a function that doesn’t really exist.
ads(12)
^
ReferenceError: ads is not defined
at Object.<anonymous> (/Users/jangwoo/Desktop/Zi/js_conf/errors-in-javascript/index.js:11:1)
at Module._compile (module.js:652:30)
at Object.Module._extensions.. js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
Copy the code
All these lines that follow may seem intimidating, but they’re actually meant to help you. This is a stack trace of the sequence of function calls that led to the error. When you run your program, even if it’s small, and it’s not really that simple, there’s a lot of work going on before you call it, and the code you write is just a small part of it — a complex JavaScript system.
A complex system of programs whose execution brings your program to life. So, here you can see that the problem is in my file, the next line is where my code is called, the third line is where the second place is called, and we can go a little bit further. It’s like tracking footsteps backwards — there’s a problem, but we can step back and see if we can find out why.
console.log(10 *pi)
Copy the code
We assume that the whole underlying system works, so the problem is in our code.
When one of your functions calls another, you will see the sequence of calls in the stack trace. Stack trace.
ReferenceError can also occur for other constants: for example, if you have 10 * PI in your code. But PI doesn’t exist – you didn’t create this constant with the name PI in advance. Then you get a ReferenceError. ReferenceError is like calling a person by the wrong name.
Type Error
The next mistake is when you confuse one thing with another. Look at this code.
let length = 12;
length(3)
Copy the code
First, we create a constant length. It’s kind of like giving something a name, in this case the number 12 and giving it the name length, or it’s kind of understandable that the length container contains the number 12. Then on the second line, we call a function length and pass in an argument 3. But wait! Length is not a function but a number! Numbers are not functions, and this is where JavaScript complains. This is a type error — the type of the thing you are using is wrong. The JavaScript interpreter won’t tell you what this thing is, but it will tell you what it isn’t, which is that length isn’t a function.
Having a typo error is like asking your cat to do laundry. You might want to ask your roommate. All of these errors — Syntax, Reference and Type errors — are about the wrong words. And all of this is unambiguous — you’ll see the error message, and you’ll know exactly why.
- Syntax Error, for which some measures are to replace, delete, or add symbols. Usually the problem is with parentheses and quotes: unclosed parentheses or unclosed quotes, parentheses or quotes should be closed.
- Reference Error, which checks if the thing you refer to exists. Maybe you used the wrong variable name, or maybe you forgot to create the variable.
- Type Error, to make sure you’re using the right thing. Often the problem is a simple confusion: you create a numeric constant and a functional constant. But then you try to call numbers. You might want to call a function.
Log Error
The last type of error we’re going to talk about today is the worst, and that’s logical error. For example, we are writing a function that converts Fahrenheit temperature to Celsius temperature. To convert Fahrenheit to Celsius Celsius, subtract 32 and multiply by 5/9. For example. (50° F-32) x 5/9 = 10°C.
Looks good, right? Let’s run it, convert 50 degrees and print it.
const fahrToCelsius = (fahr) = > {
return fahr + 32 * 5/9
}
Copy the code
Run the program and get 32.222222222222 instead of 10. Why is that? JavaScript makes no objection and runs this code with no errors. Computers don’t know what we’re doing, so they just do what we ask them to do. But the calculation result is wrong, there is a logical error. I have to subtract 32, and then I have to multiply by 5/9, but I don’t have parentheses, so it’s 32 times 5/9, and then I subtract the temperature in Fahrenheit.
It is 100% the programmer’s responsibility to combat logic errors, which can be hard at times, but hopefully brings great comfort and satisfaction in the end.