![]() ![]() To avoid situations like this, you can perform case-insensitive comparisons. ![]() This means that freeCodeCamp is not equal to FreeCodeCamp because the first letter f is lowercase for one and uppercase for the other. When comparing with the strict equality operator, it is essential to know that this comparison is case sensitive. How to Perform Case Insensitive Comparison You can also directly compare a string to a variable and a string to a string if you wish: let string1 = "freeCodeCamp" Ĭonsole.log(string1 = "codeCamp") // falseĬonsole.log(string1 = "freeCodeCamp") // trueĬonsole.log("codeCamp" = "freeCodeCamp") // false let string1 = "freeCodeCamp" Ĭonsole.log(string1 = string2) // false The strict operator is best used to compare strings in JavaScript because it checks to be sure both operands and values are the same and then returns a boolean result. It does not only check if the values are the same, but it also checks the operands: let a = 12 Strict equality, or three equality ( =) as its symbol implies, is a more detailed comparison than loose equality ( =). How to Compare Strings in JavaScript With the Strict Equality Operator In this article, you will learn how to compare strings in JavaScript. In JavaScript, you can compare strings based on their value, length, character case, and lots more. In the next chapters, we’ll concentrate on primitive values and once we’re familiar with them, we’ll move on to objects.When writing code or building a solution, you might need to compare two strings to see if they are the same before proceeding with an operation.įor example, when a user signs in, you'll want to compare the username the provide to the one in your database to see if they match. For null returns "object" – this is an error in the language, it’s not actually an object.Returns a string with the name of the type, like "string".Usually used as typeof x, but typeof(x) is also possible.The typeof operator allows us to see which type is stored in a variable. object for more complex data structures.undefined for unassigned values – a standalone type that has a single value undefined. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |