Get the App
SLTechnology News&Howtos  ›  Development  › 

How does JavaScript prevent code from crashing

Shulou Source: shulou.com Published: 2022-06-01 05:04:22 11月15日 Update

This article mainly introduces JavaScript how to prevent code crash, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Prevent code from crashing

If there is unpredictable behavior in the code, the consequences are unpredictable, so it needs to be dealt with.

For example, when the attribute you want to get is undefined or null, you will get a TypeError error.

If your project code does not support optional chaining, you can do this:

Const found = [{name: "Alex"}] .find (I = > i.name = 'Jim'); console.log (found.name); / / TypeError: Cannot read property' name' of undefined

Can be avoided in this way

Const found = [{name: "Alex"}] .find (I = > i.name = 'Jim') | {}; console.log (found.name); / / undefined

However, it depends, and there is nothing wrong with dealing with small-scale code. You don't need much code to deal with it.

Thank you for reading this article carefully. I hope the article "how to prevent JavaScript from code crashing" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Code articles processing support nothing unpredictable value interest at the same time consequences small rules small scale attributes situations more friends knowledge editing industry behavior Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux MySQL Huawei NVidia