# JavaScript - Odd or even number using bit manipulation

## JavaScript Bit Manipulation: Exercise-5 with Solution

Write a JavaScript program to check a given number is odd or even using bit manipulation.

A number (i.e., integer) expressed in the decimal numeral system is even or odd according to whether its last digit is even or odd. That is, if the last digit is 1, 3, 5, 7, or 9, then it is odd; otherwise it is even-as the last digit of any even number is 0, 2, 4, 6, or 8.

**Test Data:**

(1) -> 1 is an odd number.

(4) -> 4 is an even number.

(9) -> 9 is an odd number.

("15") -> Parameter value must be number!

**Sample Solution: **

**JavaScript Code:**

```
const check_even_odd = (n) => {
if (typeof n != "number")
{
return 'Parameter value must be number!'
}
if ((n ^ 1) == (n + 1))//even
return n +' is an even number.'
else //odd
return n +' is an odd number.'
}
console.log(check_even_odd(1))
console.log(check_even_odd(4))
console.log(check_even_odd(9))
console.log(check_even_odd("15"))
```

Sample Output:

1 is an odd number. 4 is an even number. 9 is an odd number. Parameter value must be number!

**Flowchart: **

**Live Demo: **

See the Pen javascript-bit-manipulation-exercise-5 by w3resource (@w3resource) on CodePen.

* To run the code mouse over on Result panel and click on 'RERUN' button.*

**Improve this sample solution and post your code through Disqus**

**Previous:** Next power of two of a given number.

**Next:** Check a number is a power of 4 or not.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

## JavaScript: Tips of the Day

**function and arguments**

const person = { name: 'Lydia Hallie', hobbies: ['coding'], }; function addHobby(hobby, hobbies = person.hobbies) { hobbies.push(hobby); return hobbies; } addHobby('running', []); addHobby('dancing'); addHobby('baking', person.hobbies); console.log(person.hobbies);

The addHobby function receives two arguments, hobby and hobbies with the default value of the hobbies array on the person object.

First, we invoke the addHobby function, and pass "running" as the value for hobby and an empty array as the value for hobbies. Since we pass an empty array as the value for y, "running" gets added to this empty array.

Then, we invoke the addHobby function, and pass "dancing" as the value for hobby. We didn't pass a value for hobbies, so it gets the default value, the hobbies property on the person object. We push the hobby dancing to the person.hobbies array.

Last, we invoke the addHobby function, and pass "bdaking" as the value for hobby, and the person.hobbies array as the value for hobbies. We push the hobby baking to the person.hobbies array.

After pushing dancing and baking, the value of person.hobbies is ["coding", "dancing", "baking"]

Ref: https://bit.ly/2Hcpkm6

**Exercises: Weekly Top 16 Most Popular Topics**- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- JavaScript basic - Exercises, Practice, Solution
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- JavaScript conditional statements and loops - Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation