JavaScript learning summary

preface

Recently, I have been using JavaScript for front-end and back-end development (the back-end uses the Express framework), which is a very interesting programming language. Discovery during exploration MDN document (can choose Chinese) is a good learning material, so start reading and make a simple summary. At the end of the article, some links are attached to help you learn JS/ES/Express.

Introduction to JavaScript

JavaScript (JS) is a lightweight, interpretive or just in time programming language with function priority. Although it is famous as a scripting language for developing Web pages, it is also used in many non browser environments, such as node JS, Apache CouchDB and Adobe Acrobat. JavaScript is a dynamic scripting language based on prototype programming and multi paradigm, and supports object-oriented, imperative and declarative (such as functional programming).

What is ECMAScript

The standard for JavaScript is ECMAScript (ES). As of 2012, all modern browsers fully support Es5 1. The old version of browser supports at least ES3 standard. In 2015, ECMA International released the sixth edition, officially called ECMAScript 2015, which is usually called ECMAScript 6 or ES6. Since then, ECMAScript has issued a new standard once a year. This document currently covers the latest ECMAScript draft, ECMAScript 2020.

Node. What is JS

Node.js is an open source and cross platform JavaScript runtime environment that runs the V8 JavaScript engine (Google Chrome kernel) outside the browser. Front end developers who write JavaScript for browsers can now write server-side code in addition to client-side code.
Node.js applications run in a single process without creating a new thread for each request. Its standard library provides a set of asynchronous I/O native functions (to prevent JavaScript code from being blocked). When node JS when performing I/O operations (such as reading from the network, accessing the database or file system), node JS will resume the operation when the response returns, instead of blocking the thread and wasting CPU cycle waiting.
Node. The package manager npm of JS is the largest open source library ecosystem in the world.

What is Express

Express is a flexible node that maintains a minimum size JS Web application development framework provides a set of powerful functions for Web and mobile applications.

Basics

Declarations

- var
- let: block-scoped
- const: block-scoped, read-only named constant

Comments

- one line: //
- multi-line: 
/* ...
	...
*/

Data types

  • 7 primitives:
    Boolean, Number, BigInt, String, Symbol (ES6), null, undefined
  • Object

Converting strings to numbers:

  • parseInt()
  • parseFloat()
  • + str

e.g.,

parseInt('101', 2) // 5
(+'1.1') + (+'1.1') // 2.2

Literals

  • Fixed values, not variables.
  • Types:
    Array, Boolean, Floating-point, Number, Object, RegExp, String

Function hoisting

Only function declarations are hoisted, but not function expressions.

e.g.,

/* Function declaration */
foo(); // "bar"
function foo() {
  console.log('bar');
}

/* Function expression */
baz(); // TypeError: baz is not a function
var baz = function() {
  console.log('bar2');
};

Control Flow and Error Handling

Conditional statements

if (condition_1) {
  statement_1;
} else if (condition_2) {
  statement_2;
} else {
  statement_last;
}

Falsy values:

  • false
  • undefined
  • null
  • 0
  • NaN
  • Empty string ("")

Switch statements

switch (expression) {
  case label_1:
    statements_1
    [break;]
  case label_2:
    statements_2
    [break;]
    ...
  default:
    statements_def
    [break;]
}

Exception handling statements

  • throw
  • try...catch...finally

e.g.,

function f() {
  try {
    console.log(0);
    throw 'bogus';
  } catch(e) {
    console.log(1);
    return true;    // this return statement is suspended
                    // until finally block has completed
    console.log(2); // not reachable
  } finally {
    console.log(3);
    return false;   // overwrites the previous "return"
    console.log(4); // not reachable
  }
  // "return false" is executed now
  console.log(5);   // not reachable
}
console.log(f()); // 0, 1, 3, false
function f() {
  try {
    throw 'bogus';
  } catch(e) {
    console.log('caught inner "bogus"');
    throw e; // this throw statement is suspended until
             // finally block has completed
  } finally {
    return false; // overwrites the previous "throw"
  }
  // "return false" is executed now
}

try {
  console.log(f());
} catch(e) {
  // this is never reached!
  // while f() executes, the `finally` block returns false,
  // which overwrites the `throw` inside the above `catch`
  console.log('caught outer "bogus"');
}

// OUTPUT
// caught inner "bogus"
// false

Error objects

function doSomethingErrorProne() {
  if (ourCodeMakesAMistake()) {
    throw (new Error('The message'));
  } else {
    doSomethingToGetAJavascriptError();
  }
}
⋮
try {
  doSomethingErrorProne();
} catch (e) {               // NOW, we actually use `console.error()`
  console.error(e.name);    // logs 'Error'
  console.error(e.message); // logs 'The message', or a JavaScript error message
}

Loops and Iteration

TBC...

References

MDN - JavaScript Guide

Useful Learning Resources

Basic JS:

Use OO with JS:

express.js:

Tags: Javascript Programming ECMAScript

Posted by wwfc_barmy_army on Sun, 01 May 2022 09:06:59 +0300