JavaScript Custom conditional operator in Javascript

Say I have the following construct appearing frequently in my code,

if (e.which==13){ ... }

...and I would like to make the code look a bit cleaner by writing instead:

ifEnterPressed{ ... }

Is there a way of defining an operator such as ifEnterPressed in Javascript?

Update

I have figured out that the basic issue is that Javascript does not support syntactic macros.

Answer:1

Beside the solution with a function, you can use an object for grouping and keeping the keycodes, like

KEY = {
    enter: 13
}

Usage:

if (e.which === KEY.enter){ ... }
Answer:2

No you can't define a custom operator like that.

You can of course write a helper method though:

function enterPressed(e) {
    return e.which==13;
}

And then:

if(enterPressed(e)) {
    // yay!
}
Answer:3

I have a function like this inside a NodeJs app function dojob(url){ var resultList = []; request (url, function (error, response, html){ if(!error){ var $ = cheerio.load(html); $(...

I have a function like this inside a NodeJs app function dojob(url){ var resultList = []; request (url, function (error, response, html){ if(!error){ var $ = cheerio.load(html); $(...

  1. nodejs cheerio function

I am trying to convert two strings to same format like toUpperCase/toLowerCase to compare two strings regardless of case sensitive in javaScript. Below is my function. function submitForm() { ...

I am trying to convert two strings to same format like toUpperCase/toLowerCase to compare two strings regardless of case sensitive in javaScript. Below is my function. function submitForm() { ...

  1. uncaught typeerror cannot read property
  2. uncaught typeerror cannot read property 'addeventlistener' of null
  3. uncaught typeerror cannot read property 'length' of undefined
  4. uncaught typeerror cannot read property 'style' of null
  5. uncaught typeerror cannot read property 'fn' of undefined
  6. uncaught typeerror cannot read property 'type' of undefined
  7. uncaught typeerror cannot read property 'value' of null
  8. uncaught typeerror cannot read property 'appendchild' of null
  9. uncaught typeerror cannot read property '0' of undefined
  10. uncaught typeerror cannot read property 'style' of undefined
  11. uncaught typeerror cannot read property 'mdata' of undefined
  12. uncaught typeerror cannot read property 'setstate' of undefined
  13. uncaught typeerror cannot read property 'call' of undefined
  14. uncaught typeerror cannot read property 'top' of undefined
  15. uncaught typeerror cannot read property 'replace' of undefined
  16. uncaught typeerror cannot read property 'map' of undefined
  17. uncaught typeerror cannot read property of null
  18. uncaught typeerror cannot call a class as a function

i have this site: link There is a difference when you call function in document.ready calculation or resize function There is a white border when the page loads calculation is not correct ... $( ...

i have this site: link There is a difference when you call function in document.ready calculation or resize function There is a white border when the page loads calculation is not correct ... $( ...

Below is my code for the directive cabinet and my two controllers CabinetThumbnails,ModalCtrl. Based on my requirement i am using the directive cabinet and the CabinetThumbnails to render couple of ...

Below is my code for the directive cabinet and my two controllers CabinetThumbnails,ModalCtrl. Based on my requirement i am using the directive cabinet and the CabinetThumbnails to render couple of ...