JavaScript Basics
Data Types
-
Primitive data types
- number // var a = 5;
- boolean // var c = true;
- string // var b = “hello”;
- null // var d = null
- undefined // var e = undefined
-
Data Types
- Object
Numbers
Predefined values
- Infinity
- NaN
- Number.MAX_VALUE
- Number.MIN_VALUE
- Number.NaN
- Number.POSITIVE_INFINITY
- Number.NEGATIVE_INFINITY
NaN
- Result of erroneous operations (for example
"string" / 2
) - Any arithmetic operation with NaN as an input will have NaN as a result
- NaN is not equal to anything, including NaN
- Use IsNaN() to determine whether a value is NaN
var number = "string" / 2;
if (!isNaN(number)) {
// todo something
}
Boolean
A JavaScript Boolean represents one of two values: true or false.
False | True |
---|---|
false | "false" |
null | "0" |
undefined | Boolean("false"); |
"" | |
0 | |
Number.NaN |
Null and Undefined
string | number | boolean | |
---|---|---|---|
undefined | "undefined" | NaN | false |
null | "null" | 0 | false |
Object
Objects can be created using the Object()
constructor or the object initializer / literal syntax (const object1 = { a: 'foo', b: 42, c: {} };
).
Object.prototype has following methods:
- constructor
- toString()
- valueOf()
- hasOwnProperty(“name”)
- propertyIsEnumerable(“name”)
- isPrototypeOf(“object”)
Monkey patching: Object.prototype.x = 10;
Array
- Array inherits from Object
- Indexes are converted to strings and used as names for retrieving values
- Arrays have a special length member (
array.length
)
var array = new Array(1, 2, 3);
var array = [1, 2, 3];
var array = [
1,
"234234",
true,
function () {
alert("hello!");
},
];
array.length;
// Removes the element, but leaves a hole in the numbering.
delete array[number];
Handy Array methods:
- var arrayAsString = array.join("separator");
- array.reverse();
- array.sort(/_ options: comparison function _/);//important
- var newArray = array.concat("array");
- var subarray = array.slice(“startIndex”,”lastIndex”);
- array.splice(“startIndex”,”itemsToRemove”,/new items/);
- var newArrayLength = array.push(“value”);
- var removedValue= array.pop();
- var newArrayLength = array.unshift(“value”) ;
- var removedValue= array.shift();
Operators
"typeof" operator
To check if variable is defined typeof foo !== ‘undefined’
typeof | Result |
---|---|
undefined | "undefined" |
null | "object" |
true | "boolean" |
new Boolean(true) | "object" |
5 | "number" |
new Number(5) | "object" |
“foo” | "string" |
new String(“foo”) | "object" |
[1, 2, 3] | "object" |
function foo() | "function" |
Any other object | "object" |
"+" operator
if both (lef and right) are numbers
then
add them as numbers
else
convert both to string
concatenate them
For example:
"$" + 3 + 4; //'$34'
"$" + (3 + 4); //'$7'
Equal "==" and and not equal "!=" operators
- These operators can do type coercion
- It is always better to use === and !==, which do not do type coercion.
"" == "0"; // false
0 == ""; // true
0 == "0"; // true
false == "false"; // false
false == "0"; // true
false == undefined; // false
false == null; // false
null == undefined; // true
"\t\r\n" == 0; // true
JavaScript Scopes
JavaScript has following scopes.
Block Scope
Was introduced with ES6 (2015) along with important new JavaScript keywords (let
and const
) to ensure that variables, which declared inside a {}
block are not accesable from outside the block.
{
let localVar = 2;
}
// localVar is undefined
Function Scope
Variables declared within a JavaScript function has funcition scope.
function Foo() {
var localVar = "local variable with function scope";
}
Global Scope
A variable declared outside a function, becomes GLOBAL.
let carName = "Volvo";
// code here can use carName
function myFunction() {
// code here can also use carName
}