JavaScript-Discord.JS |如何检查变量是否与字符串相同

JavaScript-Discord.JS |如何检查变量是否与字符串相同,javascript,discord.js,Javascript,Discord.js,我试图找到一种方法来检查字符串是否与变量相同。 例如: const config = require("./config.json"); const amount1 = config.amount1; const amount2 = config.amount2; // How would I check if amount1 and amount2 are the same as the variable "invite.code" 如果是这种情况,我想设置另一个变量,如图所示 var a

我试图找到一种方法来检查字符串是否与变量相同。 例如:

const config = require("./config.json");
const amount1 = config.amount1;
const amount2 = config.amount2;
// How would I check if amount1 and amount2 are the same as the variable "invite.code" 
如果是这种情况,我想设置另一个变量,如图所示

var amount = ?;
如果amount1是invite.code,我希望amount1是amount1。
我尝试过谷歌,但并没有任何东西对我有所帮助或显示。

使用
const
效果很好。坚持下去。下面是如何使用
const
执行此操作

const amount1 = config.amount1;
const amount2 = config.amount2;

const amount = amount1 === invite.code ? amount1 : amount2
这是一个好主意

一般形式为:

const const_name = condition ? value_if_true : value_if_false

能很好地使用
const
用法。坚持下去。下面是如何使用
const
执行此操作

const amount1 = config.amount1;
const amount2 = config.amount2;

const amount = amount1 === invite.code ? amount1 : amount2
这是一个好主意

一般形式为:

const const_name = condition ? value_if_true : value_if_false

您可能想尝试阅读Javascript简介教程。它将涵盖“条件语句”(if/else语句)和许多其他您在初次开始时可能会有疑问的内容。它将涵盖“条件语句”(if/else语句)和许多其他您可能会在第一次开始时对其产生疑问的内容。考虑到OP不知道if/else语句是什么,三元运算符可能有点高级。如果他从未学习过,那就好了。因为这样他就不会被引诱使用
var
作为常量值。。。。因为如果你在这里使用if/else,你将被迫错误地将一个常量值设置为可变的
var
,除非你将if包装在函数的奇点后面。考虑到OP不知道if/else语句是什么,三元运算符可能有点高级。如果他从未学会它,那就好了。因为这样他就不会被引诱使用
var
作为常量值。。。。因为如果你在这里使用if/else,你会被迫错误地将一个常量值设置为可变的
var
,除非你将if包装在函数的奇异性后面。