Created
April 10, 2019 16:22
-
-
Save jpkbeddu/3f4aaf12af54d21b45ec18081468d5b8 to your computer and use it in GitHub Desktop.
Javascript Basics
This file contains 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
// 'use strict'; | |
// a = 1; // throws error due to Strict Mode | |
console.log(''); | |
console.log(''); | |
console.log('===================='); | |
console.log('Learn JS: Basics'); | |
console.log('===================='); | |
console.log(''); | |
console.log(''); | |
/** | |
* Variables and assignments | |
* | |
* | |
*/ | |
let a; // declaration - I can be anything | |
// console.log('a ===>', a); | |
let b = 'some string'; // declaration with assignment | |
b = 23; | |
// console.log('b ===>', b); | |
const CANNOT_CHANGE_ME_AFTER_ASSIGNMENT = 1; // const declared along with assignment always | |
// console.log('CANNOT_CHANGE_ME_AFTER_ASSIGNMENT ===>', CANNOT_CHANGE_ME_AFTER_ASSIGNMENT); | |
// CANNOT_CHANGE_ME_AFTER_ASSIGNMENT = 3; // throws error | |
let c; // Declaration and assignment separate | |
c = 23; // possible due to let. Try this with cosnt | |
// console.log('c ===>', c); | |
/** | |
* Primitive Data types | |
* | |
* | |
*/ | |
// Boolean | |
const isJavascriptAwesome = true; | |
// console.log('isJavascriptAwesome ===>', typeof isJavascriptAwesome); | |
// Null | |
const iAmNull = null; | |
// console.log('iAmNull ===>', typeof iAmNull); | |
// Undefined | |
const iAmNothing = undefined; | |
// console.log('iAmNothing ===>', typeof iAmNothing); | |
// Number | |
const taskCount = 23; | |
// console.log('taskCount ===>', typeof taskCount); | |
// String | |
const fullName = 'Kannan'; | |
// console.log('fullName ===>', typeof fullName); | |
// Symbol - Lets learn about this later | |
// Object | |
const iAmObject = { x: 23, y: 'ok', z: false, a: null, b: undefined, c: { qwerty: '!!!' } }; | |
// console.log(JSON.stringify(iAmObject)); | |
// console.log('>>', iAmObject.hasOwnProperty('b')); | |
// console.log('iAmObject ===>', typeof iAmObject); | |
// console.log('iAmObject.y ===>', typeof iAmObject.y); | |
// console.log('iAmObject.y ===>', typeof iAmObject.y); | |
// console.log('iAmObject.hasOwnProperty(y) ===>', iAmObject.hasOwnProperty('y')); | |
/** | |
* Arrays | |
* | |
* | |
*/ | |
// Array - Not a primitive and it itself an object | |
const myArray = [1, 'John', fullName, taskCount, null, undefined, iAmObject, {}]; | |
// console.log('myArray ===>', typeof myArray); | |
// console.log('length of ===>', myArray.length); | |
// console.log('Array.isArray(myArray) ===>', Array.isArray(myArray)); | |
/** | |
* Function declarion and invocation | |
* | |
* | |
*/ | |
// I will get hosited automatically | |
function doSomething() { | |
// console.log('doSomething is running'); | |
return 'I did something. Maybe ?'; | |
} | |
let doSomethingOutput = doSomething(); | |
// console.log('doSomething() ===>', doSomethingOutput); | |
const doSomethingVariable = function() { | |
return 'I still did something. Maybe ?'; | |
}; | |
// console.log('doSomethingVariable() ===>', doSomethingVariable()); | |
const doSomethingArrow = () => { | |
return 'I still did something via arrow function.'; | |
}; | |
// console.log('doSomethingArrow() ===>', doSomethingArrow()); | |
/** | |
* Function scoping | |
* | |
* | |
*/ | |
let myName = 'Outer Scope'; | |
// console.log('myName outside function ===>', myName); | |
function scopedVariables(myInput1, myInput2) { | |
// console.log('myName inside function before re-declaration ===>', myName); // throws error due to scoped let variable | |
let myName = 'Inner Scope'; | |
// myName = 'Changed inside inner scope'; | |
// console.log('myName inside function ===>', myName, myName2); | |
return myName; | |
} | |
myName = scopedVariables(2, 'data'); | |
let pop123 = scopedVariables(10, 'myname'); | |
// console.log('myName outside function after ===>', myName); | |
/** | |
* Operators | |
* | |
* | |
*/ | |
// unary plus + | |
// unary negation - | |
// multiplication * | |
// division / | |
// addition + | |
// subtraction - | |
// assignment = | |
// Reminder % | |
// Exponent ** | |
// Increment ++ | |
// Decrement -- | |
// Bitwise LATER | |
// Comma LATER | |
// Comparison > < >= <= == === != !== | |
// Conditional if/else Ternary | |
// Logical && || ! | |
/** | |
* More Functions | |
*/ | |
function doSomethingProductive() { | |
const myWallet = 0; | |
let salary = 1000; | |
let previousSavings = 200; | |
let expenses = 500; | |
const bankBalance = myWallet + salary - previousSavings - expenses; | |
return bankBalance; | |
// return { bankBalance: bankBalance, myWallet, salary, previousSavings, expenses }; | |
} | |
// console.log('doSomethingProductive ===>', doSomethingProductive()); | |
/** | |
* Loops | |
* | |
* | |
*/ | |
let myWallet = 100; | |
let movieLanguage = 'TAMIL'; | |
while (myWallet > 80) { | |
// console.log('I can go for a movie'); | |
let costForAMovie; | |
if (movieLanguage == 'ENG') { | |
costForAMovie = 5; | |
// console.log('I am watching ENG movie'); | |
} else { | |
costForAMovie = 4; | |
// console.log('I am watching Someother lang movie'); | |
} | |
myWallet = myWallet - costForAMovie; | |
} | |
// do { | |
// // loop body | |
// } while (condition); | |
// for (begin; condition; step) { | |
// // ... loop body ... | |
// } | |
// let xyz = 123; | |
// console.log(xyz > 300); | |
// xyz++; | |
// for (let xyz = 123; xyz < 300; xyz = xyz + 5) { | |
// console.log('Each Iteration', xyz); | |
// } | |
/** | |
* Examples | |
* | |
* | |
*/ | |
// console.log('calcSumOfNumbersArray ===>', calcSumOfNumbersArray([5, 2, 10])); | |
let numbersArray = [5, 2, 10]; | |
function calcSumOfNumbersArray(numArray) { | |
// console.log('calcSumOfNumbersArray Function is running with parameters>>>>', numArray); | |
let _sum = 0; | |
for (let index = 0; index < numArray.length; index++) { | |
const element = numArray[index]; | |
// console.log('Sum before addition', _sum); | |
_sum = _sum + element; | |
// console.log('Sum after addition', _sum); | |
} | |
// Write same loop in while, do-while | |
return _sum; | |
} | |
let myOutput = calcSumOfNumbersArray(numbersArray); | |
// console.log('>>>>>', myOutput); | |
console.log(''); | |
console.log(''); | |
console.log('===================='); | |
console.log('END'); | |
console.log('===================='); | |
console.log(''); | |
console.log(''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment