This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's declare a variable named "emailAddress" | |
let emailAddress = '[email protected]'; | |
// If accidentally we redeclare anathor variable with the same name, | |
// then the compiler will throw an error, which is indeed a great practice. | |
let emailAddress = 'abc'; | |
// Uncaught SyntaxError: Identifier 'emailAddress' has already been declared | |
// Now let's declare a variable within a loop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's declare a variable named "facebookURL" | |
const facebookURL = 'https://www.facebook.com/'; | |
// If you try to change its value then the compiler or web browser will throw an error | |
facebookURL = 'facebook.com'; | |
// Uncaught TypeError: Assignment to constant variable. | |
// Now let's declare a variable within a loop | |
for(const j = 0; j < 10; j++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a regular function | |
function addTwoNumbers(x, y) { | |
return (x + y); | |
} | |
// This is an arrow function | |
// There's an important syntactical difference to note: | |
// The arrow functions use the => instead of the "function" keyword. | |
const addTwoNumbers = (x, y) => x + y; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's declare a variable of type array | |
let numbers = [1, 2, 3]; | |
// Let's write a function with old style | |
function calculateNumbers() { | |
this.total = 0; | |
numbers.forEach(function(singleNumber) { | |
// Here the "this" keyword is pointing to the wrong location. | |
// The "this" keyword is pointing towards the current function instead of outer function. | |
this.total += singleNumber; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's create a new array of Programming Languages | |
const programmingLanguages = [ | |
'JS', 'Phython', 'PHP', 'JAVA', 'Kotlin' | |
]; | |
// Declare an empty variable that will store the programmming languages | |
// in comma separated format | |
let languages = ''; | |
// Use the for/of loop to iterate through the array of values |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's declare an array of DC heros | |
let dcHeros = [ | |
'Superman', 'Batman', 'Wonder Woman' | |
]; | |
// Now let's declare another array of Marvel heros and merge both the arrays | |
// The spread operator will extract the values of the previous array (dcHeros) | |
// to the current array. | |
let marvelAndDcHeros = [ | |
'Captain America', 'Ironman', 'Hulk', ...dcHeros |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's create a new map object | |
const userDetails = new Map(); | |
// Now assign some data in key-value pair | |
// To assign data use the set() function | |
userDetails.set('firstname', 'Bettie'); | |
userDetails.set('lastname', 'Whitlock'); | |
userDetails.set('age', 24); | |
// To get a key of the above declared map, use the get() function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can create a JavaScript Set by: | |
// → Option 1: Passing an Array to new Set() | |
const languages = new Set(['Javascript', 'Python', 'PHP']); | |
// → Option 2: Create a Set and use add() to add values | |
const languages = new Set(); | |
languages.add('Javascript'); | |
languages.add('Python'); | |
languages.add('PHP'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's create a function to calculate the sum of all values provided. Values can be indefinite. | |
// args is the name for the array | |
let addAllNumbers = (...args) => { | |
// Initialize the total value with zero. | |
let total = 0; | |
// Loop through each numbers provided and add them | |
for (let number of args) { total += number; } | |
// Return the total value. | |
return total; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's declare a constant with name "user". | |
const user = 'John'; | |
// Another constant with name "age". | |
const age = '24'; | |
// Let's see how we can use the above information to print in the console | |
// using the template literals | |
const message = `Welcome ${user}! Your age is ${age}`; |
OlderNewer