Q:

How to combine lambda and javascript forEach loops?

Here's what I've tried and for some reason the event listeners are not getting called:
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>Some sample text inside body
    <div id="log"></div>
    <script>
        var clipboardEventsHandler = ['beforecut', 'cut', 'beforecopy', 'copy', 'paste'].forEach((evt) => {
            document.addEventListener(evt, (e) => {
                log.innerHTML += ("-" + e.type + "-");
                log.innerHTML += (document.queryCommandSupported(e.type) ? "-" + e.type + " supported-" : "-" + e.type + " **not** supported-");
                log.innerHTML += (document.queryCommandEnabled(e.type) ? "-" + e.type + " enabled-" : "-" + e.type + " **not** enabled-");
                log.innerHTML += (document.queryCommandState(e.type) ? "-" + e.type " state:true-" : "-" + e.type + " state:false/null-");
                log.innerHTML += "<br>"
            })
        });
    </script>
</body>
</html>

I'm suspecting that the type of evt parameter passed to addEventListener. I confirmed that its a string by logging typeof evt before passing it to addEventListener. And its says its string. But still the event listeners are not getting called when I do copy a portion of the text inside body. Can someone tell me what's that I'm doing wrong?

A:

First- you had a syntax error - missing + here:
e.type) ? "-" + e.type " state:true-"
                       ^----

Second - it's better access elements using document.getElementById and not directly by their id:
document.getElementById('log')

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>Some sample text inside body
    <div id="log"></div>
    <script>
        var log = document.getElementById('log');
        var clipboardEventsHandler = ['beforecut', 'cut', 'beforecopy', 'copy', 'paste'].forEach((evt) => {
            document.addEventListener(evt, (e) => {
                log.innerHTML += ("-" + e.type + "-");
                log.innerHTML += (document.queryCommandSupported(e.type) ? "-" + e.type + " supported-" : "-" + e.type + " **not** supported-");
                log.innerHTML += (document.queryCommandEnabled(e.type) ? "-" + e.type + " enabled-" : "-" + e.type + " **not** enabled-");
                log.innerHTML += (document.queryCommandState(e.type) ? "-" + e.type + " state:true-" : "-" + e.type + " state:false/null-");
                log.innerHTML += "<br>"
            })
        });
    </script>
</body>
</html>

