diff --git a/README.md b/README.md index 1b3a383..bcc622f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # javascript-references -Material to reference when working with JavaScript + +Material to reference when working with JavaScript diff --git a/hello-world/README.md b/hello-world/README.md new file mode 100644 index 0000000..962c72f --- /dev/null +++ b/hello-world/README.md @@ -0,0 +1,3 @@ +# Hello World in JavaScript + +A set of scripts that output the message "Hello World". Such a program is very simple in most programming languages, and is often used to illustrate the basic syntax of a programming language. See https://en.wikipedia.org/wiki/%22Hello,_World!%22_program diff --git a/hello-world/index.js b/hello-world/index.js new file mode 100644 index 0000000..f93ad8a --- /dev/null +++ b/hello-world/index.js @@ -0,0 +1,22 @@ +/** + * No need to "use strict" + * In this case even though we are not declaring this code as an ES6 module + * the following code contains no syntax that can be caught by strict mode. + * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode + * See ./varDeclaration.js for an example that can benefit from strict mode. + */ + +/** + * The Console object provides access to the browser's debugging console + * See https://developer.mozilla.org/en-US/docs/Web/API/console + * The Console object's methods include log() + * The Console method log() outputs a message to the console. + * The message is not limited to a single string. + * See https://developer.mozilla.org/en-US/docs/Web/API/Console/log + */ +console.log("Hello World"); + +/** + * The easiest way to test this file is to run the following line in your console: + * node index.js + */ diff --git a/hello-world/varDeclaration.js b/hello-world/varDeclaration.js new file mode 100644 index 0000000..ee30c26 --- /dev/null +++ b/hello-world/varDeclaration.js @@ -0,0 +1,36 @@ +/** + * "use strict" in this case + * We are not declaring this code as an ES6 module and the following code + * contains syntax that can be caught by strict mode. + * See ./index.js for an example that does not benefit from strict mode. + */ +"use strict"; + +/** + * The code below will declare a global string named helloWorld, + * this is valid ES5 code, in ES6 we can also use let or const. + * Normally in ES6 this would be declared as a const. + * See ./es6.js for an example using const. + */ +var helloWorld = "Hello World"; + +/** + * Strict mode will catch an undeclared variable i.e: + * helloWorld = "Hello World" + * The above commented line will throw an error in strict mode. + */ + +/** + * The Console object provides access to the browser's debugging console + * See https://developer.mozilla.org/en-US/docs/Web/API/console + * The Console object's methods include log() + * The Console method log() outputs a message to the console. + * The message is not limited to a single string. + * See https://developer.mozilla.org/en-US/docs/Web/API/Console/log + */ +console.log(helloWorld); + +/** + * The easiest way to test this file is to run the following line in your console: + * node varDeclaration.js + */ diff --git a/hello-world/varExport.js b/hello-world/varExport.js new file mode 100644 index 0000000..20de3d9 --- /dev/null +++ b/hello-world/varExport.js @@ -0,0 +1,35 @@ +/** + * Do not include "use strict" + * In this case we are declaring this code as an ES6 module, + * module code is always strict mode code. + * See http://www.ecma-international.org/ecma-262/6.0/#sec-strict-mode-code + * See ./varDeclaration.js for an example that is not in strict mode by default. + */ + +/** + * The code below will declare and export a string constant named helloWorld, + * this is valid ES6 code, in ES5 we can only use var. + * See ./varDeclaration.js for an example using var. + */ +export const helloWorld = "Hello World"; + +/** + * Strict mode will catch an undeclared variable i.e: + * helloWorld = "Hello World" + * The above commented line will throw an error in strict mode. + */ + +/** + * The Console object provides access to the browser's debugging console + * See https://developer.mozilla.org/en-US/docs/Web/API/console + * The Console object's methods include log() + * The Console method log() outputs a message to the console. + * The message is not limited to a single string. + * See https://developer.mozilla.org/en-US/docs/Web/API/Console/log + */ +console.log(helloWorld); + +/** + * The easiest way to test this file is to run the following line in your console + * nodemon varDeclaration.js + */ diff --git a/hello-world/varImport.js b/hello-world/varImport.js new file mode 100644 index 0000000..f91e1f1 --- /dev/null +++ b/hello-world/varImport.js @@ -0,0 +1,28 @@ +/** + * Do not include "use strict" + * In this case we are declaring this code as an ES6 module, + * module code is always strict mode code. + * See http://www.ecma-international.org/ecma-262/6.0/#sec-strict-mode-code + * See ./varDeclaration.js for an example that is not in strict mode by default. + */ + +/** + * The code below will declare and import a string constant named helloWorld. + * See ./varExport.js + */ +import { helloWorld } from "./varExport"; + +/** + * The Console object provides access to the browser's debugging console + * See https://developer.mozilla.org/en-US/docs/Web/API/console + * The Console object's methods include log() + * The Console method log() outputs a message to the console. + * The message is not limited to a single string. + * See https://developer.mozilla.org/en-US/docs/Web/API/Console/log + */ +console.log(helloWorld); + +/** + * The easiest way to test this file is to run the following line in your console: + * nodemon varDeclaration.js + */