
توی این مقاله قصد دارم از مقایسه دو متغیر با هم بگم. همین اولش بزار یه چیزی ازتون بپرسم! شما برای مقایسه برابری دو متغیر از چه عملگری استفاده میکنید؟ از == یا === یا Object.is؟ من می خوام هر سه این مورد یه بار مقایسه کنم تا ببینیم بهتر از کدوم یکی توی شرط هامون استفاده کنیم.
توی مقایسه دو متغیر دو موردی که باید بهش دقت کرد
با توجه به این دو نکته باید ببینیم کدوم یکی از این شرط های برابری برای ما بهتر هست تا ازش بیشتر استفاده کنیم.
== یا عملگر برابری انتزاعی
بررسی میکنه که فقط مقادیر برابر هستند یا خیر! کاری هم به نوع دو متغیر نداره! به این مثال ها دقت کنید :
console.log(1 == "1") // prints true console.log(true == "true") // prints true console.log(NaN == "NaN") // prints false console.log(NaN == NaN) // prints false as NaN is can never be equal to NaN console.log(-1 == 1) // prints false But, console.log(-0 == 0) // prints true console.log({"name": "Arwa"} == {"name": "Arwa"}) // prints false as both have different //references in memoryBut, let a = {"name": "Arwa"} let b = a console.log(a == b) // prints true as both have same memory reference
نکات کلی از == :
=== یا عملگر برابری دقیق
علاوه بر مقدار دو متغیر نوع اونها هم بررسی میکنه! اگر هر دو مقدار و نوع عبارات سمت چپ و راست برابر باشند ، این عملگر درست برمی گردد.به این مثال ها دقت کنید :
console.log(1 === "1") // prints false as both are different types (Number vs String) console.log(true === "true") // prints false as both are of different types (Boolean vs String) console.log(true === true) // prints true as both are of same type (Boolean vs Boolean) and value console.log(NaN === NaN) // prints false as NaN is can never be equal to NaN console.log(null === null) // prints true console.log("Arwa" === "arwa") // prints false as string comparison is case sensitive console.log(-0 === 0) // prints true here as well console.log({"name": "Arwa"} === {"name": "Arwa"}) // prints false as well in this case as both have different references in memory let a = {"name": "Arwa"} let b = a console.log(a === b) // prints true as both have same memory reference
نکات کلی از === :
Object.is()
Syntax: Object.is(value 1, value 2)
جزو تغییرات جدید es5 هستش.با استفاده از قوانین زیر بررسی می کنه که آیا این دو مقدار یکسان هستن یا نه!
قانون 1_ هر دو مقدار undefined باشن
let a; let b; Object.is(a,b) // returns true
قانون 2_ هر دو مقدار رشته باشن،طولشون یکی باشه و کاراکتر به کاراکتر یکی باشن(منظورم حروف کوچیک و بزرگ هستش)
Object.is("Comparison Operators", "Comparison Operators") // returns true however, Object.is("Comparison Operators","comparison operators") // returns false as it is case sensitive
قانون 3_ هر دو مقدار null باشن
Object.is(null, null) // returns trueBut, Object.is(null, "null") // returns false as no type casting is done
قانون 4_ هر دو مقدار از یه Object با مرجع یکسان باشن
let a = {"name": "Arwa"} let b = aObject.is(a, b) // returns true Object.is({"name": "Arwa"}, {"name": "Arwa"}) // returns false Object.is(window, window) // returns true as both refer to the same global variable window
قانون 5_ هر دو مقدار یک عدد غیر صفر و غیر NaN باشن
Object.is(1, 1) // returns true
قانون 6_ هر دو مقدار یا 0+ هستند یا 0-
Object.is(0, 0) // returns true But, Object.is(0, -0) // returns false unlike == or === operator
قانون 7_ هر دو NaN باشن
Object.is(NaN, NaN) // returns true unlike == or === operator Object.is(NaN, 0/0) // returns true Think of this as doing isNaN() check for each value.
فقط فراموش نکنید Object.is() توسط Internet Explorer پشتیبانی نمیشه!
به انتهای مقاله امروز رسیدیم، امیدوارم از این مقاله استفاده کامل ببرید.
ممنون از توجهتون :)