MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 123 lines 3.6 kB view raw
1// Test equality operators == and != 2// Also tests strict equality === and !== 3 4console.log("=== Equality Operators Test ===\n"); 5 6// Test 1: Basic equality with numbers 7console.log("Test 1: Number equality"); 8let a = 5; 9let b = 5; 10let c = 10; 11 12console.log(" 5 == 5:", a == b); 13console.log(" 5 === 5:", a === b); 14console.log(" 5 != 10:", a != c); 15console.log(" 5 !== 10:", a !== c); 16console.log(" 5 == 10:", a == c); 17console.log(" 5 != 5:", a != b); 18 19// Test 2: String equality 20console.log("\nTest 2: String equality"); 21let s1 = "hello"; 22let s2 = "hello"; 23let s3 = "world"; 24 25console.log(" 'hello' == 'hello':", s1 == s2); 26console.log(" 'hello' === 'hello':", s1 === s2); 27console.log(" 'hello' != 'world':", s1 != s3); 28console.log(" 'hello' !== 'world':", s1 !== s3); 29console.log(" 'hello' == 'world':", s1 == s3); 30 31// Test 3: Boolean equality 32console.log("\nTest 3: Boolean equality"); 33let t1 = true; 34let t2 = true; 35let f1 = false; 36 37console.log(" true == true:", t1 == t2); 38console.log(" true === true:", t1 === t2); 39console.log(" true != false:", t1 != f1); 40console.log(" true !== false:", t1 !== f1); 41console.log(" false == false:", f1 == false); 42 43// Test 4: Undefined and null 44console.log("\nTest 4: Undefined and null"); 45let u = undefined; 46let n = null; 47 48console.log(" undefined == undefined:", u == undefined); 49console.log(" undefined === undefined:", u === undefined); 50console.log(" null == null:", n == null); 51console.log(" null === null:", n === null); 52console.log(" undefined != null:", u != n); 53console.log(" undefined !== null:", u !== n); 54 55// Test 5: Mixed type comparisons 56console.log("\nTest 5: Type comparisons"); 57let num = 5; 58let str = "5"; 59let bool = true; 60 61console.log(" 5 != '5':", num != str); 62console.log(" 5 !== '5':", num !== str); 63console.log(" true != 1:", bool != 1); 64console.log(" true !== 1:", bool !== 1); 65 66// Test 6: In conditional expressions 67console.log("\nTest 6: Conditional expressions"); 68if (5 == 5) { 69 console.log(" 5 == 5 in if statement: passed"); 70} 71 72if (5 != 10) { 73 console.log(" 5 != 10 in if statement: passed"); 74} 75 76if (undefined == undefined) { 77 console.log(" undefined == undefined in if statement: passed"); 78} 79 80if (null != undefined) { 81 console.log(" null != undefined in if statement: passed"); 82} 83 84// Test 7: Loop conditions 85console.log("\nTest 7: Loop with equality"); 86let count = 0; 87for (let i = 0; i != 5; i = i + 1) { 88 count = count + 1; 89} 90console.log(" Loop with != condition, count:", count); 91 92// Test 8: Ternary with equality 93console.log("\nTest 8: Ternary operator"); 94let result1 = (5 == 5) ? "equal" : "not equal"; 95console.log(" 5 == 5 ? 'equal' : 'not equal' =>", result1); 96 97let result2 = (5 != 10) ? "not equal" : "equal"; 98console.log(" 5 != 10 ? 'not equal' : 'equal' =>", result2); 99 100// Test 9: Object equality (by reference) 101console.log("\nTest 9: Object equality"); 102let obj1 = { x: 1 }; 103let obj2 = { x: 1 }; 104let obj3 = obj1; 105 106console.log(" obj1 == obj2 (different refs):", obj1 == obj2); 107console.log(" obj1 === obj2 (different refs):", obj1 === obj2); 108console.log(" obj1 == obj3 (same ref):", obj1 == obj3); 109console.log(" obj1 === obj3 (same ref):", obj1 === obj3); 110console.log(" obj1 != obj2:", obj1 != obj2); 111 112// Test 10: Array equality 113console.log("\nTest 10: Array equality"); 114let arr1 = [1, 2, 3]; 115let arr2 = [1, 2, 3]; 116let arr3 = arr1; 117 118console.log(" arr1 == arr2 (different refs):", arr1 == arr2); 119console.log(" arr1 === arr2 (different refs):", arr1 === arr2); 120console.log(" arr1 == arr3 (same ref):", arr1 == arr3); 121console.log(" arr1 === arr3 (same ref):", arr1 === arr3); 122 123console.log("\n=== All tests completed ===");