Number Is Even

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will delve into the world of JavaScript programming and learn how to write a function to determine whether a given number is even or odd. The lab will guide you through the steps of using the modulo operator to check whether a number is divisible by 2, and then returning true if it is even, or false if it is odd. By the end of this lab, you will have a better understanding of basic programming concepts in JavaScript and how to write functions for simple tasks.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") subgraph Lab Skills javascript/variables -.-> lab-28419{{"`Number Is Even`"}} javascript/data_types -.-> lab-28419{{"`Number Is Even`"}} javascript/arith_ops -.-> lab-28419{{"`Number Is Even`"}} javascript/comp_ops -.-> lab-28419{{"`Number Is Even`"}} end

Check if a Number is Even

To practice coding, open the Terminal/SSH and type node. Use the following code to check if a number is even or odd:

const isEven = (num) => num % 2 === 0;

The code above uses the modulo (%) operator to check whether a number is odd or even. If the number is even, the function returns true. If it's odd, the function returns false.

Here is an example of how to use the isEven function:

isEven(3); // returns false

Summary

Congratulations! You have completed the Number Is Even lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like