try-catch-finally 1

try-catch-finally

localStorage.getItem(key)

    try {
        data = JSON.parse(key)
    }
    catch (e) {
        // if the code errors, this bit of code will run
    }
    finally {
    
        return data
    }

Here is what the above code is Doing:
1. We’re using the `localStorage.getItem()` method to get the data from the local storage.
2. We’re using the `JSON.parse()` method to convert the data from a string to an object.
3. We’re using the `try` and `catch` statements to handle any errors that might occur.
4. We’re using the `finally` statement to return the data.

Now, let’s create a function that will save the data to the local storage.

Similar Posts