Confusion of JavaScript
JavaScript’s syntax is based on the Java and C languages
Array
is a special kind of object.0, empty strings (
""
),NaN
,null
, andundefined
all becomefalse.
let
allows you to declare block-level variables. The declared variable is available from the block it is enclosed in.
const
allows you to declare variables whose values are never intended to change. The variable is available from the block it is declared in.
var
is the most common declarative keyword. It does not have the restrictions that the other two keywords have. This is because it was traditionally the only way to declare a variable in JavaScript. A variable declared with thevar
the keyword is available from the function it is declared in.
JavaScript has a similar set of control structures to other languages in the C family. Conditional statements are supported by if
and else
; you can chain them together if you like:
JavaScript objects can be thought of as simple collections of name-value pairs. As such, they are similar to:
Dictionaries in Python.
Hashes in Perl and Ruby.
Hash tables in C and C++.
HashMaps in Java.
Associative arrays in PHP.
There are two basic ways to create an empty object:
var obj = new Object();
var obj = {};
Attribute access can be chained together:
obj.details.color; // orange
obj['details']['size']; // 12
an object’s properties can again be accessed in one of two ways:
// dot notation
obj.name = 'Simon';
var name = obj.name;
And
// bracket notation
obj['name'] = 'Simon';
var name = obj['name'];
// can use a variable to define a key
var user = prompt('what is your key?')
obj[user] = prompt('what is its value?')
JavaScript is a prototype-based language that contains no class statement, as you’d find in C++ or Java (this is sometimes confusing for programmers accustomed to languages with a class statement). Instead, JavaScript uses functions as classes.
Arrays
Arrays in JavaScript are actually a special type of object.