# Swift and JavaScript comparison snippets(2) — Basic Operators

### Assignment Operator

Swift

`let b = 10var a = 5a = b`
`// decomposed tuple with multiple values into multiple variableslet (x, y) = (1, 2)print("x = \(x), y = \(y)") // x = 1, y = 2`

JavaScript

`let b = 10var a = 5a = b`
`// object matching with destructuring assignmentconst {x, y} = {x:1, y:2}console.log(`x = \${x}, y = \${y}`) // x = 1, y = 2// or array matching with destructuring assignmentconst [x, y] = [1, 2]console.log(`x = \${x}, y = \${y}`) // x = 1, y = 2`

### Arithmetic Operators

• Subtraction (-)
• Multiplication (*)
• Division (/)

Swift

`1 + 2       // equals 35 - 3       // equals 22 * 3       // equals 610.0 / 2.5  // equals 4.0"hello, " + "world"  // equals "hello, world"`

JavaScript

`1 + 2       // equals 35 - 3       // equals 22 * 3       // equals 610.0 / 2.5  // equals 4"hello, " + "world"  // equals "hello, world"`

### Remainder Operator

Swift

`9 % 4    // equals 1-9 % 4   // equals -1`

JavaScript

`9 % 4    // equals 1-9 % 4   // equals -1`

### Unary Minus/Plus Operator

Swift

`let three = 3let minusThree = -three       // minusThree equals -3let plusThree = -minusThree   // plusThree equals 3, or "minus minus three"`
`let minusSix = -6let alsoMinusSix = +minusSix  // alsoMinusSix equals -6`

JavaScript

`const three = 3const minusThree = -three       // minusThree equals -3const plusThree = -minusThree   // plusThree equals 3, or "minus minus three"`
`const minusSix = -6const alsoMinusSix = +minusSix  // alsoMinusSix equals -6`

### Compound Assignment Operators

Swift

`var a = 1a += 2 // a is now equal to 3`

JavaScript

`let a = 1a += 2 // a is now equal to 3`

### Comparison Operators

• Equal to (a == b)
• Not equal to (a != b)
• Greater than (a > b)
• Less than (a < b)
• Greater than or equal to (a >= b)
• Less than or equal to (a <= b)
• Identity operators, refer to the same object instance (a === b)
• Identity operators, not refer to the same object instance (a !== b)

Swift

`1 == 1   // true because 1 is equal to 12 != 1   // true because 2 is not equal to 12 > 1    // true because 2 is greater than 11 < 2    // true because 1 is less than 21 >= 1   // true because 1 is greater than or equal to 12 <= 1   // false because 2 is not less than or equal to 1`
`let name = "world"if name == "world" {    print("hello, world")} else {    print("I'm sorry \(name), but I don't recognize you")}// Prints "hello, world", because name is indeed equal to "world".`
`let p1 = Person();let p2 = Person();p1 === p2 // falsep1 !== p2 // true`

JavaScript

`1 == 1   // true because 1 is equal to 12 != 1   // true because 2 is not equal to 12 > 1    // true because 2 is greater than 11 < 2    // true because 1 is less than 21 >= 1   // true because 1 is greater than or equal to 12 <= 1   // false because 2 is not less than or equal to 1`
`const name = "world"if (name == "world") {    console.log("hello, world")} else {    console.log(`I'm sorry \${name}, but I don't recognize you`)}// Prints "hello, world", because name is indeed equal to "world".`
`const p1 = new Person();const p2 = new Person();p1 === p2 // falsep1 !== p2 // true`

### Ternary Conditional Operator

Swift

`let contentHeight = 40let hasHeader = truelet rowHeight = contentHeight + (hasHeader ? 50 : 20)// rowHeight is equal to 90`

JavaScript

`const contentHeight = 40const hasHeader = trueconst rowHeight = contentHeight + (hasHeader ? 50 : 20)// rowHeight is equal to 90`

### Logical Operators

• Logical NOT (!a)
• Logical AND (a && b)
• Logical OR (a || b)

Swift

`let allowedEntry = falseif !allowedEntry {    print("ACCESS DENIED")}// Prints "ACCESS DENIED"`
`let hasDoorKey = falselet knowsOverridePassword = trueif enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {    print("Welcome!")} else {    print("ACCESS DENIED")}// Prints "Welcome!"`

JavaScript

`const allowedEntry = falseif (!allowedEntry) {    console.log("ACCESS DENIED")}// Prints "ACCESS DENIED"`
`const hasDoorKey = falseconst knowsOverridePassword = trueif (enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword) {    console.log("Welcome!")} else {    console.log("ACCESS DENIED")}// Prints "Welcome!"`

# More by unbug Topics of interest