About if contains comma expression in JavaScript

About if contains comma expression in JavaScript

Sometimes you will see English commas "," in the if statement in JavaScript. This is actually a comma expression. In the if condition, only the last expression plays a role.

Consider the following example:

let a = 1,

	b = 2,

	c = 3;

// if only checks the last expression if (a == 10, b == 20, c == 3) {

	console.log("c == 3");

} else {

	console.log("c != 3")

}



// Console output:

// c == 3

There are 3 expressions in the above if statement, the first two are not true, only the last one is true, and only the last one is tested in if statement, so the result is true .

Although if only checks the last expression, the previous expressions will also be executed.

Please refer to the following code:

let a = 10,

	b = 20,

	c = 30;

// if only checks the last expression, but the previous expressions will also be executed if (a = 1, b = 2, c == 3) {

	console.log("c == 3");

} else {

	console.log("c != 3")

}

// Console output:

// c != 3



console.log(a, b); 

// Console output:

// 1 2

In the if statement, the last condition c == 3 is not true, so the if statement is false .

But the first two expressions are executed, so finally a=1 , b=2 .

This is the end of this article about if contains comma expression in JavaScript. For more related content about if contains comma expression in JavaScript, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of how JS operates on pages inside and outside Iframe
  • How to make if judgment in js as smooth as silk
  • Implementation of a simplified version of JSON.stringify and its six major features explained in detail
  • Summary of various uses of JSON.stringify
  • Vue implements online preview of PDF files (using pdf.js/iframe/embed)
  • Summary of JavaScript JSON.stringify() usage
  • Detailed explanation of how to solve the circular reference problem encountered when using JSON.stringify
  • The difference and use of json.stringify() and json.parse()
  • Selenium+BeautifulSoup+json gets the json data in the Script tag

<<:  Implementing long shadow of text in less in CSS3

>>:  Div can input content without using input as an input box to block the automatic input style

Recommend

Detailed explanation of the seven data types in JavaScript

Table of contents Preface: Detailed introduction:...

JavaScript custom calendar effect

This article shares the specific code of JavaScri...

Docker packages the local image and restores it to other machines

1. Use docker images to view all the image files ...

MYSQL updatexml() function error injection analysis

First, understand the updatexml() function UPDATE...

Vue ElementUI implements asynchronous loading tree

This article example shares the specific code of ...

JavaScript to implement mobile signature function

This article shares the specific code of JavaScri...

JavaScript determines whether the browser is IE

As a front-end developer, I can’t avoid IE’s pitf...

Detailed explanation of software configuration using docker-compose in linux

Preface This article will share some docker-compo...

Detailed analysis of the blocking problem of js and css

Table of contents DOMContentLoaded and load What ...

Learn more about using regular expressions in JavaScript

Table of contents 1. What is a regular expression...

Steps for docker container exit error code

Sometimes some docker containers exit after a per...

In-depth explanation of the locking mechanism in MySQL InnoDB

Written in front A database is essentially a shar...

How to install nginx in centos7

Install the required environment 1. gcc installat...

Introduction to Apache deployment of https in cryptography

Table of contents Purpose Experimental environmen...