22 Jun



Javascript
Python
Syntax
  • the if statement is followed by parentheses () containing the condition, and the block of code to be executed is enclosed within curly braces {}.
if (condition1) {
    // Code block
} else if (condition2) {
    // Code block
} else {
    // Code block
}
  • the if statement also uses parentheses () for the condition, but the block of code is defined by indentation.
if condition1:
    # Code block
elif condition2:
    # Code block
else:
    # Code block
Comparison Operators
  • JavaScript uses double equals == for loose equality comparison and triple equals === for strict equality comparison. 
  • Other operators include != (not equal), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).
  • Python uses double equals == for equality comparison and != for inequality comparison. 
  • Other operators include > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).
  • Python also supports is and is not for object identity comparison
Truthiness and Falsiness
  • JavaScript has several "falsy" values that evaluate to false in conditional statements, such as 
    • false
    • 0
    • '' (empty string), 
    • null
    • undefined, and 
    • NaN
  • All other values are considered "truthy" and evaluate to true.
  • In Python, the following values are considered "falsy" and evaluate to false in conditionals:
    •  False,
    • None,
    • 0,
    • 0.0,
    • '' (empty string),
    • [] (empty list),
    • {} (empty dictionary), and
    • () (empty tuple).
  • All other values are considered "truthy" and evaluate to true.
Ternary Operator
  • JavaScript supports the ternary operator ? :, which allows for concise conditional expressions.
var result = condition ? trueValue : falseValue;

ar result = x > 0 ? "positive" : "negative";
  • Python also supports the ternary operator if else for conditional expressions.
result = trueValue if condition else falseValue 

result = "positive" if x > 0 else "negative"
Switch Statement

switch (expression) {
    case value1:
        // Code block
        break;
    case value2:
        // Code block
        break;
    default:
        // Code block
        break;
}
 Python does not have a built-in switch statement. Typically, multiple if and elif statements are used to achieve similar functionality


Comments
* The email will not be published on the website.