Skip to content

Instantly share code, notes, and snippets.

@lifeinthegrid
Last active September 27, 2023 17:43
Show Gist options
  • Save lifeinthegrid/3f6dab21b92ceffa9a826fd041509433 to your computer and use it in GitHub Desktop.
Save lifeinthegrid/3f6dab21b92ceffa9a826fd041509433 to your computer and use it in GitHub Desktop.
GridLife.io-Learn-Tutorial-Javascript30
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="test.js"></script>
</head>
<body>
Hello
<script>
//console.log('test');
</script>
</body>
</html>
//=========================
//VARIABLES - INTRO
//Number, String, Null, Boolean
var bookTitle = 'Programming JS a';
console.log(bookTitle);
//=========================
//OPERATORS
var bookCost = 10 + 2;
bookCost = '$' + bookCost;
console.log(bookCost);
//=========================
//FUNCTIONS
function getBookTitle(author) {
var bookTitle = 'Programming JS ';
bookTitle += author;
console.log(bookTitle);
}
getBookTitle('John');
//=========================
//ARRAYS
var bookTitleArray = [
"foo",
"JavaScript: The Good Parts",
"Eloquent JavaScript",
"You Don't Know JS",
"Clean Code: A Handbook of Agile Software Craftsmanship",
"Introduction to Algorithms",
];
console.log(bookTitleArray[1]);
//=========================
//OBJECTS
var book = {
title: "Javascript by GridLife.io",
author: "Cory Lamle",
pubDate: 2023,
genre: "Education",
ISBN: "77-777-7",
};
console.log(book.title);
//=========================
//COMPLEX OBJECTS
var book1 = {
title: "Javascript by GridLife.io",
author: "Cory Lamle",
pubDate: 2023,
genre: "Education",
ISBN: "77-777-7",
};
var book2 = {
title: "C# by GridLife.io",
author: "Cory Lamle",
pubDate: 2023,
genre: "Education",
ISBN: "11-111-1",
};
var library = [book1, book2];
console.log(library[1].title);
//=========================
//CONTROL STRUCTURES (IF)
var book = {
title: "The Great Gatsby",
price: 15,
};
if (book.price > 10) {
console.log("I recommend buying this book!");
} else {
console.log("Consider other options.");
}
var book = {
title: "The Catcher in the Rye",
rating: 4,
};
if (book.rating >= 4.5) {
console.log("Highly recommended!");
} else if (book.rating >= 3.5) {
console.log("Good choice.");
} else {
console.log("Consider other books.");
}
var book = {
title: "The Great Gatsby",
price: 15,
rating: 4,
};
if (book.price > 10 || book.rating > 4) {
console.log("I recommend buying this book!");
} else {
console.log("Consider other options.");
}
//=========================
//CONTROL STRUCTURES (SWITCH)
var book = {
title: "The Catcher in the Rye",
price: 15,
rating: 4,
};
switch (true)
{
case book.rating >= 4 && book.price < 10:
console.log("Highly recommended!");
break;
case book.rating >= 3.5:
console.log("Good choice.");
break;
default:
console.log("Consider other books.");
}
//=========================
//LOOPS (for, forEach, while)
var books = [
{
title: "Javascript by GridLife.io",
author: "Cory Lamle",
},
{
title: "Eloquent JavaScript",
author: "Marijn Haverbeke",
},
{
title: "Clean Code: A Handbook of Agile Software Craftsmanship",
author: "Robert C. Martin",
},
{
title: "Introduction to Algorithms",
author: "Thomas H. Cormen",
},
];
// Example using a for loop
console.log("=====================\nUsing a for loop:");
for (let index = 0; index < books.length; index++) {
console.log(`Title: ${books[index].title}, Author: ${books[index].author}`);
}
// Example using a forEach loop
console.log("=====================\nUsing a forEach loop:");
books.forEach((book) => {
console.log(`Title: ${book.title}, Author: ${book.author}`);
});
// // Example using a while loop
console.log("=====================\nUsing a while loop:");
let index = 0;
while (index < books.length) {
console.log(`Title: ${books[index].title}, Author: ${books[index].author}`);
index++;
}
//=========================
//VARIABLE SCOPING
//------------------
//Global Scope
//Variables declared outside of any function or block have global scope and can be
//accessed from anywhere in your code.
const fixedVar = "I'm fixed";
//fixedVar = "I can't be changed";
console.log(fixedVar);
var globalVar = "I'm global";
function demo1()
{
var localVar = "I'm local";
console.log(globalVar); // Accessible
console.log(localVar); // Accessible
}
demo1();
console.log(globalVar); // Accessible
//console.log(localVar); // Error - localVar is not defined here
//------------------
//Block Scope
//Variables declared with `let` and `const` inside a block have block scope and are accessible
//only within that block.
function demo3() {
let letVar1 = "I'm function scoped";
if (true) {
let letVar2 = "I'm block-scoped";
console.log(letVar1); // Accessible
console.log(letVar2); // Accessible
}
//console.log(blockVar2); // Error - blockVar is not defined here
}
demo3();
//------------------
//Lexical Scope
//Inner functions have access to variables in their outer (enclosing) functions' scopes,
// creating closures.
function outer() {
var outerVar = "I'm outer";
function inner() {
console.log(outerVar); // Accessible due to lexical scope
}
return inner;
}
var closureFn = outer();
closureFn(); // Accesses outerVar from the enclosing scope
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment