Summary of uncommon operators and operators in js

Summary of uncommon operators and operators in js

Summary of common operators and operators in javascript

category

Operators

Arithmetic Operators +, –, *, /, % (modulo)
String Operators + string concatenation += string concatenation compound
Boolean Operators ! , &&, ||
Unary Operators ++ , -- , + (unary plus), - (unary minus)
Relational comparison operators < , <= , > , >= , != , == , === , !==
Bitwise Operators ~ Bitwise NOT & Bitwise AND | Bitwise OR ^ Bitwise XOR <<Left shift >>Signed right shift >>>Unsigned right shift
Assignment Operator = , compound assignments (+=, -=, *=, %=) compound bitwise assignments (~=, &=, |=, ^=, <<=, >>=, >>>=)
Object Operators .Property access, [] property or array access, new call constructor common object, delete variable property deletion, void (return undefined), in judgment property, instanceof prototype judgment
Other Operators ?: conditional operator, comma operator, () grouping operator, typeof type operator

Uncommon operators and operators in js

Nullish coalescing operator: ??

When the left operand is null or undefined, it returns the right operand, otherwise it returns the left operand.

null ?? 'huli' // huli
undefined ?? 'huli' // undefined
'' ?? 'huli' // ''
[] ?? 'huli' // []
({}) ?? 'huli' // {}
NaN ?? 'huli' // NaN
false ?? 'huli' // false
0 ?? 'huli' // 0

Logical null assignment: ??=

The logical null assignment operator (x ??= y) assigns a value to x only if it is nullish (null or undefined).

const a = { duration: 50 };

a.duration ??= 10;
console.log(a.duration);
// expected output: 50

a.speed ??= 25;
console.log(a.speed);
// expected output: 25

Logical OR: ||

If the existence is true, then it is true, , whichever is the previous

const a = 3;
const b = -2;
console.log(a > 0 || b > 0); // true

Can be a false value

null
undefined
NaN
"" and''
0
false

Logical or assignment: ||=

If yes, return; if no, assign value

const a = { duration: 50, title: '' };

a.duration ||= 10;
console.log(a.duration);
// expected output: 50

a.title ||= 'title is empty.';
console.log(a.title);
// expected output: "title is empty"

Logical AND: &&

If both exist, it is true, whichever is the latter.

const a = 3;
const b = -2;

console.log(a > 0 && b > 0);
// expected output: false

Logical AND assignment: &&=

Assign if exists

let a = 1;
let b = 0;

a &&= 2;
console.log(a);
// expected output: 2

b &&= 2;
console.log(b);
// expected output: 0

Optional chaining operator: ?.

The optional chaining operator ( ?. ) allows reading the value of a property that is deep in a chain of connected objects without having to explicitly verify that each reference in the chain is valid. The ?. operator functions similarly to the . chaining operator, except that instead of causing an error if the reference is nullish (null or undefined), the expression short-circuits to return undefined. When used with a function call, if the given function does not exist, undefined is returned.

const adventurer = {
  name: 'Alice',
  cat: {
    name: 'Dinah'
  }
};

const dogName = adventurer.dog?.name;
console.log(dogName);
// expected output: undefined

console.log(adventurer.someNonExistentMethod?.());
// expected output: undefined

Summarize

This is the end of this article about uncommon operators and operators in js. For more relevant js operators and operators, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript operators explained in detail never before
  • Summary of uncommon js operation operators
  • JavaScript Basics Operators
  • Summary of some efficient magic operators in JS
  • Stop using absolute equality operators everywhere in JS
  • Let's take a look at the same old JS operator explanation

<<:  Implementation of effective user groups and initial user groups in Linux

>>:  Detailed tutorial on installing different (two) versions of MySQL database on Windows

Recommend

Implementing shopping cart function based on vuex

This article example shares the specific code of ...

Complete steps to use vue-router in vue3

Preface Managing routing is an essential feature ...

Explanation of the process of docker packaging node project

As a backend programmer, sometimes I have to tink...

How to set up cross-domain access in IIS web.config

Requirement: The page needs to display an image, ...

Some experience in building the React Native project framework

React Native is a cross-platform mobile applicati...

Vue.js implements simple timer function

This article example shares the specific code of ...

JavaScript implementation of a simple addition calculator

This article example shares the specific code of ...

Zabbix monitors Linux hosts based on snmp

Preface: The Linux host is relatively easy to han...

Introduction to HTML basic controls_PowerNode Java Academy

The <input> tag The <input> tag is us...

MySQL sorting using index scan

Table of contents Install sakila Index Scan Sort ...

How to hide rar files in pictures

You can save this logo locally as a .rar file and...

How to insert weather forecast into your website

We hope to insert the weather forecast into the w...

How to clean up Alibaba Cloud MySQL space

Today I received a disk warning notification from...

Introducing ECharts into the Vue project

Table of contents 1. Installation 2. Introduction...