javascript运算符_了解JavaScript关系运算符

javascript运算符

The second group of possible comparisons in JavaScript (after equality operators), relational operators determine if one operand is greater or less than another. As such, they are frequently used in mathematical expressions, often in the form of comparing variables within if statements.

JavaScript中第二组可能的比较(在等号运算符之后 ), 关系运算符确定一个操作数是大于还是小于另一个。 因此,它们经常用在数学表达式中,通常以比较if语句中的变量的形式使用。

大于运算符 (Greater than operator)

Determines if the left operand is greater than the right operand. In the console:

确定左操作数是否大于右操作数。 在控制台中

5 > 3
> true

These operators are almost always used to compare numbers, but they can also be used with . When this occurs, the Unicode value of the characters in each of the strings is added together and compared:

这些运算符几乎总是用于比较数字,但也可以与一起使用。 发生这种情况时,会将每个字符串中字符的Unicode值相加并进行比较:

"Kirk" > "Picard"
> false

This can lead to very confusing results, and, as such, relational comparison of strings should be generally avoided.

这可能导致非常混乱的结果,因此,通常应避免字符串的关系比较。

少于运算符 (Less than operator)

Determines if the left operand is less than the right operand:

确定左操作数是否小于右操作数:

var romeFounded = -753;
var beijingFounded = -1100;

beijingFounded < romeFounded;
> true

If you become confused between the greater than and less than signs, remember two things:

如果您对大于和小于符号感到困惑,请记住两件事:

  1. We are always asking if what is on the left is greater, less than, or equal to whatever is on the right, and…

    我们一直在问, 左边的内容是否大于,小于或等于右边的内容 ,以及…

  2. The (> and <) symbols represent a shark’s mouth; sharks, being greedy eaters, always want to swim open-mouthed to whatever is largest.

    ( >< )符号代表鲨鱼的嘴; 鲨鱼是贪婪的食客, 总是想张大嘴游泳到最大的地方

So 4 < 3 is asking the question "is 3 bigger than 4?” or alternatively, read from left to right, "is 4 less than 3?” In that case, the answer is no: the shark wants to turn around and swim in the other direction to eat the larger meal of “4”.

所以4 < 3在问一个问题“ 3比4大吗?” 或者,从左至右读取“ 4是否小于3?” 在这种情况下,答案是否定的:鲨鱼想转身向另一个方向游泳,以吃较大的“ 4”餐。

大于或等于运算符 (Greater than or equal to operator)

Tests if the first operand is greater than or equal to the second operand:

测试第一个操作数是否大于或等于第二个操作数:

var highScore = 1100;
var userScore = 700;
var message = "";
if (userscore >= highScore) {
    message = "We have a new winner!";
} else {
    message = "Sorry, you haven't beaten or equalled the high score.";
}

小于或等于运算符 (Less than or equal to operator)

Tests if the first operand is less than or equal to the second operand:

测试第一个操作数是否小于或等于第二个操作数:

11003 <= 2
> false

It’s also common to forget whether the equals sign is written before or after the greater or less than sign. It’s written in the same order as the name of the operator: “less than or equal to” is therefore <=.

这也是常见忘记是否符号之前或大于或小于符号后面写等号 。 它以与运算符名称相同的顺序编写:因此,“小于或等于”为<=

不等于? (Not Equal To?)

Sometimes new coders will try to use <> as a way to express “not equal to” in JavaScript. Unfortunately, that isn’t supported; use the inequality or (preferably) the strict inequality operators instead.

有时,新的编码人员会尝试使用<>作为在JavaScript中表达“不等于”的方式。 不幸的是,不支持该功能。 使用不等式或(最好是) 严格的不等式运算符代替。

翻译自: https://thenewcode.com/337/Understanding-JavaScript-Relational-Operators

javascript运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值