Skip to content

Instantly share code, notes, and snippets.

View tararoutray's full-sized avatar

Tara Prasad Routray tararoutray

View GitHub Profile
// Let's declare a variable named "emailAddress"
let emailAddress = 'someone@example.com';
// 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
// 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 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;
// 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;
// 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
// 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
// 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
// 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');
// 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;
}
// 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}`;