Advanced JavaScript Basics

JavaScript has a few useful features that help you handle errors, work with dates, perform calculations, store data, and search through text.

We will look at the most useful ones in this Advanced JavaScript Basics chapter.

Error Handling

Errors can happen when JavaScript runs your code. Instead of letting an error stop the whole program, you can handle it using try...catch.

try…catch

Put the code that might cause an error inside try. If an error occurs, catch handles it.

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript Error Handling</title>
</head>
<body>

    <h1>Error Handling</h1>

    <script>
        try {
            let result = unknownVariable;
            console.log(result);
        } catch (error) {
            console.log("Something went wrong.");
        }
    </script>

</body>
</html>

Output:

Something went wrong.

The program continues instead of stopping because the error was handled by catch.

JSON

JSON stands for JavaScript Object Notation. It is a common format for storing and sending data. You will often see JSON when working with APIs.

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript JSON</title>
</head>
<body>

    <h1>JSON Example</h1>

    <script>
        let student = {
            name: "Aman",
            age: 15
        };

        let jsonData = JSON.stringify(student);

        console.log(jsonData);
    </script>

</body>
</html>

Output:

{"name":"Aman","age":15}

JSON.stringify() converts a JavaScript object into a JSON string.

To convert JSON back into a JavaScript object, use JSON.parse().

let student = JSON.parse(jsonData);

console.log(student.name);

Output:

Aman

Dates and Time

JavaScript provides the Date object for working with dates and time.

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript Date</title>
</head>
<body>

    <h1>Current Date and Time</h1>

    <script>
        let currentDate = new Date();

        console.log(currentDate);
    </script>

</body>
</html>

Output:

The output will show the current date and time, for example:

Mon Aug 17 2026 17:20:00 GMT+0530

The exact output depends on when you run the program.

You can also get individual parts of a date:

let date = new Date();

console.log(date.getFullYear());
console.log(date.getMonth() + 1);
console.log(date.getDate());

getMonth() starts counting from 0, so January is 0 and December is 11.

Math Methods

JavaScript’s Math object provides methods for common calculations.

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript Math</title>
</head>
<body>

    <h1>Math Methods</h1>

    <script>
        console.log(Math.round(4.6));
        console.log(Math.floor(4.9));
        console.log(Math.ceil(4.1));
        console.log(Math.max(10, 25, 15));
        console.log(Math.min(10, 25, 15));
    </script>

</body>
</html>

Output:

5
4
5
25
10

Some useful methods are:

  • Math.round() → rounds to the nearest whole number
  • Math.floor() → rounds down
  • Math.ceil() → rounds up
  • Math.max() → finds the largest value
  • Math.min() → finds the smallest value

Regular Expressions

A regular expression, or regex, is a pattern used to search or check text.

For example, you can use one to check whether an email contains @.

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript Regular Expression</title>
</head>
<body>

    <h1>Regular Expression</h1>

    <script>
        let email = "aman@example.com";

        let pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

        console.log(pattern.test(email));
    </script>

</body>
</html>

Output:

true

The test() method checks whether the text matches the pattern. Regular expressions are useful for checking things such as emails, phone numbers, and passwords.

Local Storage

localStorage allows a website to save small amounts of data in the browser. The data remains there even after the page is closed.

Example

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript Local Storage</title>
</head>
<body>

    <h1>Local Storage</h1>

    <script>
        localStorage.setItem("username", "Aman");

        let username = localStorage.getItem("username");

        console.log(username);
    </script>

</body>
</html>

Output:

Aman

To remove the saved value:

localStorage.removeItem("username");

Local storage is useful for things such as saving a user’s preferences or simple website data.

Key Points

  • try...catch helps handle errors.
  • JSON is commonly used to exchange data.
  • Date is used for dates and time.
  • Math provides useful calculation methods.
  • Regular expressions help search and check text.
  • localStorage saves data in the browser.

Next, we will move to Asynchronous JavaScript, where we will learn about callbacks, promises, async/await, and APIs.


Frequently Asked Questions (FAQs)

Q1. What is Advanced JavaScript Basics?

Advanced JavaScript Basics covers useful JavaScript features for handling errors, working with data, performing calculations, managing dates, searching text, and storing information in the browser.

Q2. How does JavaScript Error Handling work?

JavaScript Error Handling uses try...catch to handle errors that occur while code is running. The try block contains code that may cause an error, while catch handles the error.

Q3. What is JavaScript JSON used for?

JavaScript JSON is commonly used to store and exchange structured data, especially when working with APIs. JSON.stringify() converts an object into a JSON string, while JSON.parse() converts JSON back into an object.

Q4. What is JavaScript Local Storage?

JavaScript Local Storage allows websites to save small amounts of data in a user’s browser. Data saved with localStorage remains available even after the browser page is closed.

Q5. What are JavaScript Regular Expressions used for?

JavaScript Regular Expressions are patterns used to search, match, or validate text. They can be useful for checking emails, phone numbers, passwords, and other text formats.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top