Writing a program in JavaScript:
- Syntax: the rules to write code
- Logic: the code to process data
- Input: the data given for processing
- Output: the processed data
Printing a string in console:
- console.log("Hello World");
JavaScript Identifier:
- Identifier is a name that we give to any reference (for variables, constants, functions, etc.)
- Any existing JavaScript keyword can not be an identifier.
- The first letter of an identifier must be either a letter (A-Z, a-z) or an underscore (_), or a dollar sign ($).
- The rest of the characters can be letters, digits, underscore, or dollar signs.
JavaScript variables:
- JavaScript variables can be defined using 2 keywords. "var" and "let".
- var identifierName = "Hello";
- let identifierName = "Hello";
Difference between var and let:
- The term "let" was introduced in ES6 (ECMAScript 6) with additional features.
- The first difference is the scope of the variable. Variables defined as var will have global scope, but variables defined as let will have the block scope (only accessible inside the block they are defined).
- The second difference is the variables declared as let will not be added to the global window object, so we can't access the variable using the window object, but we can access the variables declared as var using the window object. (window.variableName)
- var age = 22;
- function checkAge(a) {
- var age = 12
- console.log(age) // prints 12
- console.log(window.age) // prints 22
- }
- variables declared as var can be redeclared, but variables declared as let can't be redeclared.
JavaScript Operators:
- Arithmetic operator (+, -, *, /, %, ++a, a++, --b, b--)
- Comparison operator (==, !=, >, <, <=, >=)
- logical operator (&&, ||, !)
- Bitwise operator (&, |, ~, ^, >>, <<, >>>)
- Assignment operator (=, +=, -+, *=, /=, %=)
- Conditional operator (condition ? statement 1 : statement 2)
- typeof operator (typeof(1) = "Number")
JavaScript loops:
- for loop : for (let i = 0; i < 10; i++) { statements } [Note: i = pointer variable]
- for in loop: for (let a in array1) { a = indexes of elements }
- for of loop: for (let a of array1) { a = elements of array }
- while loop: var i = 0; while (i < 10) { i++ }
- do while loop: var i = 0; do { statements } while(i < 10);
- break; continue;
JavaScript Conditional Statements:
- if else : if (condition) {} else {}
- Switch case: Switch(statement) { case1: statement break; case2: statement break; default: statement}
JavaScript Array:
- Array is a collection of similar type of data.
- Creating array = let a = [];
- Adding element end : a.push(element);
- Removing element end: a.pop();
- Adding element beginning: a.unshift(element);
- removing element beginning: a.shift();
- adding element at position: a.splice(position, 0, element);
- Delete element at position: a.splice(position, 1);
- array length: a.length;
- creating string with format: array.join(divider element);
- creating string with comma = array.toString();
- concat array: a.concat(["a","b"]);
- Getting index of element: indexOf, lastIndexOf;
- Higher order functions : sort, forEach, filter, every, map, reduce etc.
JavaScript String:
- String is a collection of characters.
- Creating a string: "Hello", new String("Hello");
- String length: a.length
- To get a character at index: a.charAt(0);
- To get the ASCII code of the character at index: a.charCodeAt(0);
- To combine 2 strings: a.concat(b)
- find index: indexOf(), lastIndexOf()
- Compare 2 strings: a===b; / a.localCompare(b);
- change case: toUpperCase(), toLowerCase();
- substring: substr(start, length) -> legacy (don't use), substring(start, end) -> end not included
- slicing a string: a.slice(startindex, endindex)
- splitting string: a.split(" "); a.split("");
- Replace string character: let a ="apple is round", let regex = "\apple\gi"; a.replace(regex, "orange"); => "orange is round" -> regex flags: g: global, i: ignore case, m: match over multiple line
- Matching with regex: a.match(regex)
- Higher-order functions : forEach
- important loops: for in, for of.
JavaScript Objects:
- Key value pairs
- Creating objects: {}
- Creating objects 2: let a = new Object(); a.property = value;
- converting to array: Object.keys(objectname), Object.values(objectname), Object.entries(objectname)
- Adding a new key value pair: ObjectName["new key"] = new value;
- Creating constructor function: function abc(a,b,c) {this.a = a; this.b = b; this.c = c;} calling: abc(10,20,30);
- Creating constructor function 2: function abc(value) {with (this) { a = value }} calling: abc(10);
- ex: number , boolean , string , array, math etc.
Comments
Post a Comment