0

So I have a JavaScript function I'm working on. I noticed that my editor is flagging an error on line 4 but the function works in every browser I've tested except for IE and Safari. I find this particularly weird.

Basic idea for the function is: take 'element' (the ID of a html element) and convert it to a string, define an array of all possible versions of 'element', remove 'element' from the array, and perform another function using the now-filtered array and 'element' as variables. This is what I have:

function thisFunction(element){
    var eStr=element.toString();
    var eArray=['element1', 'element2', 'element3'];
    var fArray=eArray.filter(e=>e!==eStr);
    fArray.forEach(doThis);
    function doThis(value){

        Now do this with 'fArray'....

        return false;
        doThis();
        }

    And do this with 'element'...

    return false;
    thisFunction();
    }

The error is apparently something to do with the "var fArray" line, but I cant see a problem. The error when I click the link that is supposed to activate this function is "thisFunction is undefined" as well as the error on line 4.

12
  • not really sure what to tell you it seems right to me. Although you will want to replace the var with let or const Commented Nov 14, 2017 at 23:48
  • Copy and paste the error. Commented Nov 14, 2017 at 23:49
  • What is element - "the ID of a html element" - if it's not already a string? Commented Nov 14, 2017 at 23:50
  • I know it seems right to me too... Commented Nov 14, 2017 at 23:55
  • 1
    @100000011 -- please don't edit "SOLVED" into the title of your question, or edit an answer into the question itself. If an answer has been found, it should be posted as an answer, and you can upvote or accept it if you found it helpful. On Stack Overflow that's where answers live, not in the main question! Commented Nov 15, 2017 at 19:40

1 Answer 1

1

The problem on line 4 is that IE does not support the arrow function syntax you're using in the callback. Changing this line:

var fArray=eArray.filter(e=>e!==eStr);

To something like:

var fArray=eArray.filter(function(e) { return e!==eStr; });

Should fix things.

Details on which JS features are supported by which browsers can be found at https://kangax.github.io/compat-table/es6/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.