Skip to content

Latest commit

 

History

History

Javascript

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

The latest ECMAScript standard defines seven data types:

Six data types that are primitives:

  • Boolean
  • Null
  • Undefined
  • Number
  • String
  • Symbol (new in ECMAScript 2015)

and the seventh one is Object

console.log("\"string\" type:", typeof "string"); // Logs: "string" type: string
console.log("7 type:", typeof 7); // Logs: 7 type is: number
console.log("7.5 type:", typeof 7.5); // Logs: 7.5 type is: number
console.log("true type:", typeof true); // Logs: true type: boolean
console.log("undefined type:", typeof undefined); // Logs: undefined type: undefined
console.log("null type:", typeof null); // Logs: null type: object
console.log("{} type:", typeof {}); // Logs: {} type: object
console.log("[] type:", typeof []); // Logs: [] type: object
console.log("function type:", typeof
    function() {}); // Logs: function type: function

In JavaScript, there are no true integers, all numbers are implemented in double-precision 64-bit binary format IEEE 754. When we use binary floating-point numbers, it will have some side effects. Here is an example of these side effects.

0.1 + 0.2 == 0.3 // false

For the primitive data types, when we use literals to initialize a variable, the variable only has the literals as its value, it doesn’t have a type. It will be converted to the corresponding type only when necessary.

let a = 111 // only literals, not a number
a.toString() // converted to object when necessary

Typeof

typeof can always display the correct type of primitive types, except null:

typeof 1 // 'number'
typeof '1' // 'string'
typeof undefined // 'undefined'
typeof true // 'boolean'
typeof Symbol() // 'symbol'
typeof b // b is not declared,but it still can be displayed as undefined

For object, typeof will always display object, except function:

typeof [] // 'object'
typeof {} // 'object'
typeof console.log // 'function'

Object is a reference type. We will encounter problems about shallow copy and deep copy when using it.

```js
let a = { name: 'FE' }
let b = a
b.name = 'EF'
console.log(a.name) // EF

Type Conversion

Converting to Boolean

When the condition is judged, other than undefined, null, false, NaN, '', 0, -0, all of the values, including objects, are converted to true.

Objects to Primitive Types

When objects are converted, valueOf and toString will be called, respectively in order. These two methods can also be overridden.

let a = {
    valueOf() {
        return 0
    }
}

Arithmetic Operators

Only for additions, if one of the parameters is a string, the other one will be converted to string as well. For all other operations, as long as one of the parameters is a number, the other one will be converted to a number.

Additions will invoke three types of type conversions: to primitive types, to numbers and to string:

1 + '1' // '11'

2 * '2' // 4

[1, 2] + [2, 1] // '1,22,1'

// [1, 2].toString() -> '1,2'
// [2, 1].toString() -> '2,1'
// '1,2' + '2,1' = '1,22,1'

## Can you name two programming paradigms important for JavaScript app developers?

JavaScript is a multi-paradigm language, supporting imperative/procedural programming along with OOP (Object-Oriented Programming) and functional programming. JavaScript supports OOP with prototypal inheritance.

## What is functional programming?

Functional programming produces programs by composing mathematical functions and avoids shared state & mutable data. Lisp (specified in 1958) was among the first languages to support functional programming, and was heavily inspired by lambda calculus. Lisp and many Lisp family languages are still in common use today.

## What is the difference between classical inheritance and prototypal inheritance?

Class Inheritance: instances inherit from classes (like a blueprinta description of the class), and create sub-class relationships: hierarchical class taxonomies. Instances are typically instantiated via constructor functions with the `new` keyword. Class inheritance may or may not use the `class` keyword from ES6.

Prototypal Inheritance: instances inherit directly from other objects. Instances are typically instantiated via factory functions or `Object.create()`. Instances may be composed from many different objects, allowing for easy selective inheritance.