This is a JavaScript review for C/C++ developers. It focuses on the differences, especially the most surprising ones to a C/C++ programmer. This review is based on the book called "You Don't Know JS" [1].
Objects
Objects in JavaScript are similar to C/C++ objects. An exemplary data structure:
var obj = {
a: "hello world",
b: 42,
c: true
};
can be imagined as residing in memory as:
Data structure, source:[1:2]
Note: JavaScript has typed values instead of typed variables.
That fact is visible when we create a variable, store a value in it, check the type of the value in the variable and then change the value into one of another type, checking the value type again successively.
Build-in types of values available in JavaScript are:
- string,
- number,
- boolean,
- null and undefined,
- object, and
- symbol (new in ES6).
ES6 refers to ECMAScript 6 which is the official name for the specification of JavaScript.
Referring to objects
We can refer to sub-objects in JavaScript using either bracket notation: person["name"]
or dot notation: person.name
. Dot notation is preferred for it is shorter and generally easier to read.
When using the bracket notation, the property name can be either a variable or a string literal wrapped either in " "
or ' '
braces.
There are more types in JavaScript. Two of these that are very popular are called array
and function
. Each of them should be thought of more like subtype of the object
type.
Arrays
A sample array declaration in JavaScript looks as follows:
var arr = [
"hello world",
42,
true
];
Functions
Another subtype of object
type is the function. It can be defined as follows:
function foo() {
return 42;
}
Built-in type methods
Primitive types in JavaScript have built-in type methods, eg.:
var a = 4.24456;
a.toFixed(4); // "4.2446"
Coercion
There are two types of coercion: explicit and implicit. Implicit coercion should be paid attention to because it may bring unexpected results:
var a = "42";
var b = a * 1; // "42" implicitly coerced to 42 here
a; // "42"
b; // 42 - the number!
Values "truthy" and "falsy"
When a non-boolean value is coerced to a boolean, does become true
or false
?
The specific list of "falsy" values in JavaScript is as follows:
- "" (empty string)
0
,-0
,NaN
,null
,undefined
,false
(I don't say!),
Any value that is not on the "falsy" list is "truthy".
Functions are "truthy" too! And so are objects and arrays.
Equality
Equality checking can be done in two ways in JavaScript:
==
- equality check,===
- "strict" equality check (disabling implicit coercion).
Which operator to choose?
- If either value (side) in a comparison could be a boolean value, avoid
==
and use===
instead. - If either value in compariso0n could be of those specific values:
0
,""
or[]
- an empty array, avoid==
and use===
. - In all other cases you are safe to use
==
.
Warning: Comparing objects
Objects are actually held by reference. This means that comparisons will simply check whether the references match and will not touch the underlying values.
For example, two arrays with the same contents will not be ==
-equal:
var a = [1,2,3];
var b = [1,2,3];
var c = "1,2,3";
a == c; // true
b == c; // true
a == b; // false
Concerning string inequality checking, strings are compared lexicographically, but if one of the values being compared is not of a string
type, then both values are coerced to be number
s and a standard numeric comparison occurs as specified in the standard [2].
Hoisting
All var
declarations are moved to the top of their enclosing scopes, enabling the use of functions and variables in the code "before" they have been declared.
Goodbye part 1
This is the end of part 1 of the JavaScript tutorial.