From 53e611d2b474872f55eba0258b10c72b420df79b Mon Sep 17 00:00:00 2001 From: Ken Schaefer Date: Tue, 5 Mar 2024 08:24:24 -0600 Subject: [PATCH] init into git --- Section 01 - Intro/first-app.js | 3 + Section 01 - Intro/hello.txt | 1 + Section 02 - JavaScript/syntax.js | 119 ++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 Section 01 - Intro/first-app.js create mode 100644 Section 01 - Intro/hello.txt create mode 100644 Section 02 - JavaScript/syntax.js diff --git a/Section 01 - Intro/first-app.js b/Section 01 - Intro/first-app.js new file mode 100644 index 0000000..a645704 --- /dev/null +++ b/Section 01 - Intro/first-app.js @@ -0,0 +1,3 @@ +const fs = require('fs'); + +fs.writeFileSync('hello.txt', 'Hello from Node.js'); \ No newline at end of file diff --git a/Section 01 - Intro/hello.txt b/Section 01 - Intro/hello.txt new file mode 100644 index 0000000..30f6a65 --- /dev/null +++ b/Section 01 - Intro/hello.txt @@ -0,0 +1 @@ +Hello from Node.js \ No newline at end of file diff --git a/Section 02 - JavaScript/syntax.js b/Section 02 - JavaScript/syntax.js new file mode 100644 index 0000000..a2282b5 --- /dev/null +++ b/Section 02 - JavaScript/syntax.js @@ -0,0 +1,119 @@ +/****************************************************************************** + * VARIABLES + */ + +/* Outdated syntax +var name = 'Ken'; +var age = 57; +var hasHobbies = true; +*/ + +// Using let and const for variables +const uname = 'Ken'; +let age = 57; +let hasHobbies = true; + +/****************************************************************************** + * FUNCTIONS + */ + +// Basic structure and use of a function +function basicSummarizeUser(userName, userAge, userHasHobbies) { + return ('Name is ' + userName + ', age is ' + userAge + ', and the user has hobbies: ' + userHasHobbies); +} +console.log(basicSummarizeUser(uname, age, hasHobbies)); + +// A named function +const namedSummarizeUser = function(userName, userAge, userHasHobbies) { + return ('Name is ' + userName + ', age is ' + userAge + ', and the user has hobbies: ' + userHasHobbies); +} +console.log(namedSummarizeUser(uname, age, hasHobbies)); + +// Arrow function syntax: affects the use of 'this' +const arrowSummarizeUser = (userName, userAge, userHasHobbies) => { + return ('Name is ' + userName + ', age is ' + userAge + ', and the user has hobbies: ' + userHasHobbies); +} +console.log(arrowSummarizeUser(uname, age, hasHobbies)); + +// Arrow function when there is a single statement in the function +const add = (a, b) => a + b; +console.log(add(1, 2)); + +/****************************************************************************** + * OBJECTS + */ + +const person = { + name: 'Ken', + age: 57, + + greet() { + console.log(`Hi I am ${name}`); + } +} +console.log(person) + +/****************************************************************************** + * ARRAYS + */ +const hobbies = ['Sports', 'Cooking']; +for(let hobby of hobbies) { + console.log(hobby); +} + +/****************************************************************************** + * SPREAD AND REST OPERATORS + */ + +// Simply copying an array +const copiedArray = hobbies.slice(); +console.log(copiedArray); + +// Making a copy using SPREAD +const copiedAgainArray = [...hobbies]; + +// Can use SPREAD to copy objects too +const copiedPerson = {...person} + +// REST bundles items together +const toArray = (...args) => { + return args; +}; +console.log(toArray(1, 2, 3, 4)); // The REST operator let's me hand in a variable number of arguments + +/****************************************************************************** + * DESTRUCTURING + */ + +const printName = (personData) => { + console.log(personData.name); +} +printName(person); + +// Alternative using destructuring + +const printNameD = ({name}) => { + console.log(name); +} +printNameD(person); // Still pass in the object but only the name prop is passed + +/****************************************************************************** + * ASYNC CODE and PROMISES + */ + +const fetchData = () => { + const promise = new Promise((resolve, reject) => { + setTimeout(() => { + resolve('Done'); + }, 1500); + }); + return promise; +}; + +setTimeout(() => { + console.log('Timer is done'); + fetchData().then(text => { + console.log(text); + }); +}, 2000); +