Description
Notes from this resource.
The Basics
- JS built in classes:
- Array
- Regex
- Date
- Math
- JSON
- An interesting quote: “You might think of Python as being fairly informal. There are other languages, like Java and C++ that are more formal.”
- The first version of JS was written in just 10 days ?!?!
- An interesting set of ways to define functions:
- The below definition is without hoisting, so
main()
cannot be called before the definition.
- The below definition is without hoisting, so
function main() {
alert("Hello World!")
writeln("Hello World!");
}
--------
var main = function() {
alert("Hello World!")
writeln("Hello World!");
}
- “This example also makes it clear that in JavaScript functions are just objects that can be assigned to variables.”
- “Now there is one more character on this line that is significant and that is the
;
at the end. In JavaScript the;
signifies the end of a statement. Unlike Python where statements are almost always only one line long JavaScript statements can spread across many lines. The compiler knows it has reached the end of a statement when it encounters a;
.” - “For example
(a,b) => a+b
is the equivalent offunction(a,b) { return a + b}
orlambda a,b : a+b
in Python.” - “In JavaScript, the parenthesis around the condition are required because
if
is technically a function that evaluates toTrue
orFalse
.” - Switch-case exists in JS, and can be used similarly to how
elifs
are used in Python. - JavaScript uses Java like notation for definite for loops:
for (let i = 0; i < 10; i++) {...}
- Two options for condensed for loops,
for-of
will access items in the iterable,for-in
accesses the indices.
let l = [1, 2, 1, 2, 3]
for (let i of l) {
writeln(i);
}
Output: 1 2 1 2 3
------------
let l = [1, 2, 1, 2, 3]
for(let i in l) {
writeln(l[i]);
}
Output: 1 2 1 2 3
.forEach()
also exists, and allows you to iterate over elements of an arraywhile
anddo-while
loops both exist in JS, and they’re very similar to Java’s.- Primitive types:
number, string, boolean, undefined, null, symbol
- 3 scopes in JS:
global, function, block
- Global: Any var that is declared outside of a function, and is accessible from anywhere in code.
- Function: Independent to each function. Variables defined within this scope are only accessible from within the function
- Block: definitions from
let
statements, for example.
- “The best advice I have seen is to stop using
var
and just uselet
in your code.” - JS is dynamically typed
- User input function is
prompt
. This brings up a dialog box. - JS only has a single numeric data type. It also doesn’t support infinite precision ints.
- Falsy values in JS (everything else evaluates to
true
):null
undefined
NaN
0
""
false
- JS strings are immutable, and there’s no indexing or slicing built-in operators. Instead, use methods for this, very Java-esque.
- Formatted strings are defined with back-ticks.
Left off on 9/8 at this section: 2.7. Collections