• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript If Not – Check if a Condition is False

JavaScript If Not – Check if a Condition is False

August 30, 2022 Leave a Comment

We can use the JavaScript if not statement to check if a condition is not true. Usually in a JavaScript if-statement, we are looking to see if a certain condition is true. This can be useful in certain scenarios when we only want to know if a condition is false.

Here is the simple code for an if not statement:

if( !(someValue1 > someValue2) ){
  //This will run if the condition above is false
}

In the example above, notice that we use the negation operator !. This is a key part of an if not statement. So if the value someValue1 is greater than someValue2 making that part of the condition true, the not operator would then make the statement False. Which means that the if statement would not run the code inside.

Let’s take a look at a simple example of this to help explain it further.


In this example, we will simply have an if statement compare a couple of variables that represent numbers.

var num1 = 1;
var num2 = 2;
if( !(num2 > num1) ){
  //This code will not run
}

In the above code, since num2 > num1 will return true (since 2 > 1), we negate this result and so the if statement will be false and not run.

Let’s take a look at a couple more examples.

var numsArray = [1,2,3,4,5,6,7,8,9];
if( !(numsArray.includes(10)) ){
  console.log("10 is not included in the array");
}

#Output
10 is not included in the array

The above example uses the includes() method. To learn more about this method, check out this post on it.

And one more example:

var office_workers = [
  {id: 1, firstName: "John", lastName: "Smith"}, 
  {id: 2, firstName: "Jane", lastName: "Smith"}, 
  {id: 3, firstName: "Nicole", lastName: "Williams"}, 
  {id: 4, firstName: "Bill", lastName: "Brown"}, 
  {id: 5, firstName: "Sam", lastName: "Johnson"}
];

for( var i=0; i<office_workers.length; i++ ){
  if( !(office_workers[i].lastName == "Smith") ){
    console.log(office_workers[i]);
  }
};

#Output
{id: 3, firstName: 'Nicole', lastName: 'Williams'}
{id: 4, firstName: 'Bill', lastName: 'Brown'}
{id: 5, firstName: 'Sam', lastName: 'Johnson'}

The above example will check the array of worker objects and return any that Do Not have the lastName value of “Smith”.

Hopefully this article has been useful in helping you understand the JavaScript if not condition.

Other Articles You'll Also Like:

  • 1.  JavaScript isPrime – How to Check if a Number is Prime in JavaScript
  • 2.  Using JavaScript to Round to 1 Decimal
  • 3.  JavaScript acosh – Find Hyperbolic Arccosine of Number
  • 4.  Examples Using the JavaScript += Addition Assignment Operator
  • 5.  Check if a String Contains Uppercase Letters in JavaScript
  • 6.  JavaScript Check if Attribute Exists in HTML Element
  • 7.  Subtract All Numbers in an Array Using JavaScript
  • 8.  JavaScript Random Boolean – How to Generate Random Boolean Values
  • 9.  Using JavaScript to Scroll to Bottom of Div
  • 10.  JavaScript atan – Find Arctangent and Inverse Tangent of Number

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy