Skip to content

Instantly share code, notes, and snippets.

@kissarat
Created October 27, 2021 20:30
Show Gist options
  • Save kissarat/0c6f06b3406ed7ede97b9b7e347cb961 to your computer and use it in GitHub Desktop.
Save kissarat/0c6f06b3406ed7ede97b9b7e347cb961 to your computer and use it in GitHub Desktop.
JavaScript for Python developer

JavaScript for Python developer

Javascript is dynamically typed language (same as Python) with C/C++ compaterble syntax that look at the same for Java and C# also

Boolean data type

let trueValue = true
let falseValue = false
trueValue = True
falseValue = False

Boolean operations

Binary boolean operations

true && false == false // Conjunction
true || false == true // Disjunction
true ^ true == false // XOR
(true != true) == false // Not equal
let empty = ''
(empty === falseValue) == false // Strict equal
True and False == False # Conjunction
True or False == True # Disjunction
(True != True) == False # Not equal
empty = ''
(empty == falseValue and (type(empty) == type(falseValue))) == False # Strict equal

Unary boolean operations

trueValue &= false
false == trueValue
trueValue = trueValue and False
False == trueValue

Conditional operator

let trueValue = true
let truthValue
if (trueValue) {
    truthValue = true
} else {
    truthValue = false
}
const rightValue = trueValue ? true : false
rightValue === trueValue
trueValue = True
if trueValue:
    truthValue = True
else:
    truthValue = False

from typing import Final
if trueValue:
    rightValue: Final = True
else:
    rightValue: Final = False

Numbers

let integerValue = 2
let floatValue = 0.5
integerValue * floatValue === 1
let hexNumber = 0x10
let octNumber = 010
let binaryNumber = 0b10
hexNumber == octNumber * binaryNumber
trueValue = True
integerValue = 2
floatValue = 0.5
integerValue * floatValue == 1
hexNumber = 0x10
octNumber = 0o10
binaryNumber = 0b10
hexNumber == octNumber * binaryNumber

Array and loop

const last = 9
let digits = []
for(let i = 0; i <= last; i++) {
    digits.push(i)
}
digits.push(null)
console.log(digits)
for(const i of digits) {
    try {
        console.log(`${i}: ${i == digits[i]}`)
    } catch (err) {
        console.error(err)
    } finally {
        console.log(`Value ${i}: ${digits[i]}`)
    }
}
let j = digits.length - 1
while(j >= 0) {
    if (typeof digits[j] === 'number')
        digits[j] = last - j
    else
        digits.splice(j, 1)
}
last = 9
digits = []
for i in range(10):
    digits.append(i)

digits.append(None)
print(digits)
for number in digits:
    try:
        print('%i: %s' % (number, number == digits[number]))
    except Exception as err:
        print(err)
    finally:
        print('Value %s' % (number))

j = len(digits) - 1
while j >= 0:
    if type(digits[j]) == int:
        digits[j] = last - j
    else:
        digits = digits[:j] + digits[j+1:]
    j -= 1

print(digits)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment