Search code examples
javascriptmodulodivide

Find if variable is divisible by 2


How do I figure out if a variable is divisible by 2? Furthermore I need do a function if it is and do a different function if it is not.


Solution

  • Use modulus:

    // Will evaluate to true if the variable is divisible by 2
    variable % 2 === 0