Recording the Way

Differences of JavaScript With Python

2017/12/26

Expecting Loose Comparison

In regular comparison, data type does not matter. This if statement returns true:

var x = 10;
var y = “10”;
if (x == y)

In strict comparison, data type does matter. This if statement returns false:

var x = 10;
var y = “10”;
if (x === y)

It is a common mistake to forget that switch statements use strict comparison:

This case switch will display an alert:

var x = 10;
switch(x) {
​ case 10: alert(“Hello”);
}

This case switch will not display an alert:

var x = 10;
switch(x) {
​ case “10”: alert(“Hello”);
}

Misunderstanding Floats

All numbers in JavaScript are stored as 64-bits Floating point numbers (Floats).

All programming languages, including JavaScript, have difficulties with precise floating point values:

var x = 0.1;
var y = 0.2;
var z = x + y // the result in z will not be 0.3

To solve the problem above, it helps to multiply and divide:

Example

var z = (x 10 + y 10) / 10; // z will be 0.3 —- (same with python)

closing (ending) statements with semicolon is optional in JavaScript.

Accessing Arrays with Named Indexes

Many programming languages support arrays with named indexes.

Arrays with named indexes are called associative arrays (or hashes).

JavaScript does not support arrays with named indexes.

In JavaScript, arrays use numbered indexes:

In JavaScript, objects use named indexes.

Undefined is Not Null

With JavaScript, null is for objects, undefined is for variables, properties, and methods.

To be null, an object has to be defined, otherwise it will be undefined.

Expecting Block Level Scope

JavaScript does not create a new scope for each code block.

It is true in many programming languages, but not true in JavaScript.

This code will display the value of i (10), even OUTSIDE the for loop block:

Example

for (var i = 0; i < 10; i++) {
​ // some code
}
return i;

Built-in JavaScript Constructors

JavaScript has built-in constructors for native objects:

Example

var x1 = new Object(); // A new Object object
var x2 = new String(); // A new String object
var x3 = new Number(); // A new Number object
var x4 = new Boolean(); // A new Boolean object
var x5 = new Array(); // A new Array object
var x6 = new RegExp(); // A new RegExp object
var x7 = new Function(); // A new Function object
var x8 = new Date(); // A new Date object

The Math() object is not in the list. Math is a global object. The new keyword cannot be used on Math.

Did You Know?

As you can see, JavaScript has object versions of the primitive data types String, Number, and Boolean.

There is no reason to create complex objects. Primitive values execute much faster.

And there is no reason to use new Array(). Use array literals instead: []

And there is no reason to use new RegExp(). Use pattern literals instead: /()/

And there is no reason to use new Function(). Use function expressions instead: function () {}.

And there is no reason to use new Object(). Use object literals instead: {}

CATALOG
  1. 1. Expecting Loose Comparison
  2. 2. Misunderstanding Floats
    1. 2.1. Example
  3. 3. closing (ending) statements with semicolon is optional in JavaScript.
  4. 4. Accessing Arrays with Named Indexes
  5. 5. Undefined is Not Null
  6. 6. Expecting Block Level Scope
    1. 6.1. Example
  7. 7. Built-in JavaScript Constructors
    1. 7.1. Example
  8. 8. Did You Know?