skip to Main Content

I have the below code:

    if (typeof profileChart !== undefined) {
        profileChart.destroy();
    } else {
        let profileChart = new Chart(
           pisteElevationProfileChart,
           configPiste
        );
    }

Despite the solution provided here, I still get an error as follows;

script.js?ver=1.0:336 Uncaught ReferenceError: profileChart is not defined

So how can you test for an undefined variable.

4

Answers


  1. Here’s your problem:

    if (typeof profileChart !== undefined) {
    

    The typeof value is never going to be undefined. You need to check against typeof undefined.

    if (typeof profileChart !== typeof undefined) {
    

    Explanation:

    typeof profileChart
    

    This piece of code will evaluate to the string "undefined" if the variable profileChart is not defined.

    "undefined" !== undefined
    

    This statement will always evaluate to True.

    typeof undefined
    

    will evaluate to "undefined".

    "undefined" !== "undefined"
    

    evaluates to False so the else block would be executed when the variable is not defined.

    Login or Signup to reply.
  2. Others explained the typeof issue.

    The reference error you get when you do if (profileChart) is because you need to DECLARE the variable before you can test it for falsy values.

    let a;
    if (a) // undefined
    if (b) // referenceError
    

    You declare it and wrap it like this

    let profileChart; // This will hold the current instance of the chart
    
    const getProfileChart = (pisteElevationProfileChart, configPiste) => {
      // Destroy the existing chart if it exists
      profileChart?.destroy(); // optional chaining
      // Create a new chart and return it
      return new Chart(pisteElevationProfileChart, configPiste);
    };
    
    // before you want to use it
    
    profileChart = getProfileChart(pisteElevationProfileChart, configPiste);
    
    Login or Signup to reply.
  3. The problem can be slightly deeper in the code. What is the line where you declare the variable and what keyword do you use (var, let, or const)? The scope where you are declaring the variable also matters. If, for example, you declare a variable inside an if statement (something like the following:

    if(statement) {
       const profileChart = //...;
    }
    
    if (typeof profileChart !== undefined) {
        profileChart.destroy();
    }
    //...
    

    ), then the variable will never be accessible (ReferenceError is the error where variable has not been initialized yet and so it can’t be accessed for the type checking). The solution that can work here is:

    let profileChat;
    if(statement) {
       profileChart = //...;
    }
    
    if (profileChart) {
        profileChart.destroy();
    }
    //...
    

    Hope this is well, but it’s better to rearrange this sort of code in favor of the const declaration

    Login or Signup to reply.
  4. the issue is that the typeof operator returns a string representing the type of the operand so when checking if a variable is undefined you need to compare the result of typeof with the string undefined

    if (typeof profileChart !== 'undefined') {
        profileChart.destroy();
    } else {
        let profileChart = new Chart(
           pisteElevationProfileChart,
           configPiste
        );
    }
    

    best approach would be to declare profileChart with an initial value of undefined outside of your if/else block.

    let profileChart;
    
    // some other code where profileChart might be defined
    
    if (profileChart !== undefined) {
        profileChart.destroy();
    } else {
        profileChart = new Chart(
           pisteElevationProfileChart,
           configPiste
        );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search