در حال بارگذاری ...

Operator Differences Overview0:00

PHP and JavaScript share a lot of similarities when it comes to operators, but of course there are some differences. I say there are some, there are a lot of differences, and I want to focus on the differences because, well, that's really what you need to know. We can assume that everything is going to work fine, but if you know what the differences are, then, well, you can expect that. The first difference PHP has the Spaceship operator, JavaScript doesn't, so don't even try to use it. And we're going to start with the plus sign because that's where a lot of the differences come into play. And we already know that we can use the plus sign to concatenate strings together. So if we take string1 plus the string of comma world, we know that we are concatenating hello and world, and we end up with a string of hello world. But as you've probably guessed, if you're going to use a plus sign, things can be a little bit different, especially if you're using a string. Because in PHP, the plus sign is strictly for arithmetic. There's no room for any kind of strange result, which you can get with JavaScript.

Template Strings Syntax1:07

Because in php, the plus sign is strictly for arithmetic. There's no room for any kind of strange result, which you can get with JavaScript. So what we now have are what are called template strings. You can think of them as herodox in php because you can use variables inside of them. You can also span multiple lines. So a template string is denoted with backticks. And then to use a variable or an expression, you use the dollar sign followed by a pair of curly braces. And then inside of the curly braces, you have your variable name or whatever JavaScript expression that you want to use. And in this case, there's no concatenation. You are essentially creating this template, and JavaScript is going to insert the values that you want.

Plus Sign Type Coercion1:48

And in this case, there's no concatenation. You are essentially creating this template, and JavaScript is going to insert the values that you want. So in this case, we end up with a string of hello world, and we didn't have to do any kind of concatenation, which is great. Because if we say 1 plus the string of 2, well, in PHP, we would end up with the value of 3 because the plus sign only performs arithmetic operations. Not so in JavaScript. It's either arithmetic or it is string concatenation. And since we are dealing with a string here, JavaScript is going to return the string of 12. So it is going to look at both operands. It sees a number. It sees a string.

It sees a number. It sees a string. So it's going to coerce the number into a string so that it will concatenate the two together. And it's going to do this every time there is a string involved. That's just how it is. However, it does so on a left-to-right basis. So now we have 1 plus 2 plus the string of 2, in which case that's going to give us the value of a string of 32. Because, once again, it's going to go from left to right. The first operands are the numeric value of 1 and the numeric value of 2. So it's addition.

The first operands are the numeric value of 1 and the numeric value of 2. So it's addition. It's going to add those two together to be 3. The next operand or the next operation is taking that value of 3 plus the string of 2. So it's going to coerce that 3 into the string of 3 and then concatenate them together. And then anything that comes after the string is automatically going to be coerced into a string. So we could have even an array of 1, 2, 3, in which case we would end up with a string that is 3, 2, 1, 2, 3. So this is why we've wanted to get away from concatenation. Because any time that we have a plus sign and we have a string operand, the result is going to be a string, and that might not be what we intended.

Because any time that we have a plus sign and we have a string operand, the result is going to be a string, and that might not be what we intended. Although, if we are concatenating things, we might think that. But, for the most part, if you want to build a string, use a template string, just because that's really the great way to go about it now. And use the plus sign for arithmetic, not for string concatenation. Unless if you just absolutely need that. Now, if we do 1 minus the string of 2, well, that's going to give us what we would expect, the value of negative 1. Because subtraction, or the minus sign, only does subtraction. So therefore, JavaScript is going to see a number in a string,

Comparison Coercion Rules4:18

Because subtraction, or the minus sign, only does subtraction. So therefore, JavaScript is going to see a number in a string, so it's going to coerce the string into a numeric value so that it can perform that arithmetic. The same is true for multiplication and division. It's only the plus sign where things are weird. Alright, so, the same kind of thing is going to happen with comparison operators. For example, if we have 1 less than the string of 2, well, the less than operator is an arithmetic comparison operator. So, the numeric value of 1 and the string value of 2. It's going to coerce the string into a number and then perform the comparison.

So, the numeric value of 1 and the string value of 2. It's going to coerce the string into a number and then perform the comparison. So, in this particular case, that's not false, that's true. However, if JavaScript cannot coerce a string into a numeric value, then it just defaults to false. And it doesn't matter what the sign is. It can be less than, it can be greater than. It is still going to result in false. Except if we have an empty string. So, we have 1 greater than an empty string, that's true.

Except if we have an empty string. So, we have 1 greater than an empty string, that's true. Because in JavaScript, an empty string is what we would call falsy. It is the equivalent of 0. So, in this case, it is going to be coerced into the value of 0. Therefore, it is going to be true in this particular case. We'll talk about truthy and falsy, well, I guess right now, since we're talking about comparisons. So, let's talk about falsy, because there's certainly a lot more falsy values.

Truthy and Falsy Values5:57

So, let's talk about falsy, because there's certainly a lot more falsy values. So, an empty string would be a falsy value, meaning that if you were going to use this in some kind of comparison, it would be evaluated as false, or the number 0. So, the number 0 is falsy. Let's see, an empty array is falsy. Let's see, the 0 string is falsy. The value of null is falsy, as well as the undefined value. And then, of course, false is falsy.

The value of null is falsy, as well as the undefined value. And then, of course, false is falsy. I think that's it. And everything else is considered truthy. So, any string that is not 0 is truthy. The value of true is, of course, truthy. An array that has at least one element is truthy. An object is truthy. If it's not listed here, it's truthy. So, going back to our comparison here,

If it's not listed here, it's truthy. So, going back to our comparison here, if we were to do 1 greater than an array, I'm pretty sure that's going to be true, because an empty array is falsy. And since we are dealing with a numeric operation, you know, greater than, less than, then the falsy value will be evaluated as 0. And we can see this in the browser. Let's just go to the console.

And we can see this in the browser. Let's just go to the console. We'll say 1 is greater than an empty array, and sure enough, the result is true. So, I'm very glad I did not lie to you. And so, now that we know what is truthy and falsy, we can start to talk about equality. So, we'll start with just simple equality. The numeric value of 1 is equal to the string value of 1, because for simple equality,

Equality vs Identity7:44

The numeric value of 1 is equal to the string value of 1, because for simple equality, JavaScript is going to look at both operands and it's going to coerce them into the same type. So, in this case, the numeric value of 1 is equal to the string value of 1. But a lot of times, we want to be sure that we are working with both not just the same value, but also the same type, in which case we have the === (triple equal sign).

but also the same type, in which case we have the ===. And it only returns true if the value and the type are the same. And so, this is useful if you need to do some kind of comparison with the number 0. Because 0 is falsy, that means it can be equal to false, it can be equal to an empty array, it can be equal to anything that is falsy.

it can be equal to an empty array, it can be equal to anything that is falsy. But if you want to treat the number 0 as the number 0, then you would want to use the identity operator. That's what the triple equal sign is. It is checking if both operands are identical. And, of course, if we have equality, we have inequality, which looks exactly like it does in PHP.

Increment, Spread, Rest8:55

we have inequality, which looks exactly like it does in php. So, this would be false because 1 is equal to 1, but 1 is not identical to the string of 1, so that would be true. So, let's see, what else do we have? We have increment and decrement, which are largely going to work the same. So, that's ++ or --, both in front of or behind

So, that's ++ or --, both in front of or behind whatever value that we want to increment and decrement. It's going to work essentially the same. And then we also have spread. So, if we have array1, which will have 1, 2, and 3, we can spread that out into another array using ...array, and then we can build upon that with 4, 5, and 6,

using ..., array, and then we can build upon that with 4, 5, and 6, which that gives us an array of 1, 2, 3, 4, 5, 6. And that works like it does in PHP. But the same syntax or the same operator, although I guess technically it's not the same operator, would be the rest operator, to where we could pass in multiple values, and that would let us treat them as an array. So, in this case, args.reduce,

and that would let us treat them as an array. So, in this case, args reduce, and we want to take a and b and add them together. We haven't talked about functions, but we do have arrow functions, which is something that we will, of course, talk about whenever we talk about functions. That way, we can pass 1, 2, as many arguments as we want so that we can add them all together in that particular case. And that works, you know, like it does in php.

so that we can add them all together in that particular case. And that works, you know, like it does in php. So, oh, we need to talk about the logicals, which there's really no difference there. The ands, the ors, same thing. So, I guess we don't need to talk about them. I think that does it for our talk on operators. In the next episode, we will talk about flow control with if statements and loops and things like that.

with if statements and loops and things like that.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟