Posts

javascript maps

  JavaScript Maps ( Map Object) 1) What is a Map ? A Map is a built-in JavaScript object used to store key–value pairs where: Keys can be any data type (string, number, object, function). Insertion order is preserved. It is different from a plain object {} . 2) Create a Map const map = new Map(); With initial values: const map = new Map ([   [ "name" , "Sushant" ],   [ "age" , 23 ],   [ true , "active" ] ]); 3) Common Methods set(key, value) map.set("city", "Pune"); get(key) map.get("name"); // "Sushant" has(key) map.has("age"); // true delete(key) map.delete("age"); size map.size; // number of entries clear() map.clear(); 4) Looping over a Map for...of for (let [key, value] of map) { console.log(key, value); } keys(), values(), entries() map.keys(); map.values(); map.entries(); 5) Map vs Object (Important) Feature Map Object Key types Any String / Symbol Order Preserved Not gu...

most used coding symbols

 Here’s a list of most used coding symbols (characters) along with their common names and uses in programming: Symbol Name Common Use in Programming = Equals / Assignment Operator Assigns value to variable == Equality Operator Compares two values != Not Equal Checks inequality > Greater Than Comparison < Less Than Comparison >= Greater Than or Equal Comparison <= Less Than or Equal Comparison + Plus Addition or string concatenation - Minus Subtraction * Asterisk / Star Multiplication / Slash / Forward Slash Division % Modulus Remainder after division ++ Increment Increases value by 1 -- Decrement Decreases value by 1 && Logical AND Used in conditions ` ` ! Logical NOT Negation & Bitwise AND Bitwise operations ` ` Bitwise OR ^ Caret / XOR Bitwise XOR ~ Tilde Bitwise NOT -> Arrow Accessing struct/class members (C/C++) . Dot Access obj...

LCM & HCF- Tricks

  Trick-1 As learned in the previous video, the greatest number that will exactly divide x, y, & z is the HCF of x, y, & z. If asked to find out the greatest number that will divide x, y, & z leaving reminders a, b, & c, respectively, the number would be HCF of (x-a), (y-b), and (z-c). Example-  Find the greatest number that will divide the 204 and 327 leaving remainders 4 and 7, respectively. Solution:  Greatest number will be HCF of (204-4) and (327-7) HCF of 200 and 320 = 40 Therefore, 40 will divide 204 and 327 leaving remainders 4 and 7, respectively.   Trick-2 As learned in the previous video, the least number which when divided by x, y, and z leave no remainder is LCM of x, y, and z. The least number which when divided by x, y, and z leave remainders a, b and c, respectively is given by [(LCM of x, y, & z) - k]. Where, k = x-a = y-b = z-c Example-  Find the least number divided by 12, 15, and 20 leaves remainders 4, 7, and 12, respecti...

DOM in JavaScript:

Image
  DOM stands for Document Object Model. It is an Application Programming Interface that facilitates the logical layout of content and allows us to create or modify a document. In simple words, one can create/modify/delete contents using DOM. In the HTML document, DOM views it as a tree of different nodes, where each node is one particular HTML element. Note: DOM is not a programming language. It is a way to access or modify data.   How does DOM work? When a browser reads the HTML document, it creates a representational tree with the nodes along with ways/methods to access different nodes in the tree. Example of the representational tree: There are majorly four elements in DOM: Documents, elements, text, attributes. DOM provides functions in order to access various DOM elements in the tree. Some commonly used ones are: getElementById(): Access the first element with the specified id. getElementByName(): Accesses all the elements with the specified name getElementByTagName():Acc...

Functions in JavaScript: Closures in JavaScript:

Image
  Functions in Javascript: There may be a certain piece of code that you would want to run multiple times in your program. In such cases, instead of writing the same code several times, it is better to wrap that code inside something called a function and then reuse that function as and when needed. Functions can be defined in Javascript using the following syntax: function  function_name  (parameters) { lines of code  }   We use the  function  keyword followed by the function name of our own choice. Parameters are certain values which may be required inside a function. These values are passed while calling that function. Note: Parameters are optional, a functional may or may not have parameters.   What does calling the function mean? Calling a function simply means using that function whenever needed. It is also called invoking a function.  Examples: Function with parameters: Function without parameters: Returning values from a function: Sup...

Data types in JavaScript: Difference between ‘==’ and ‘===’ in Javascript:

Image
  Data types in JavaScript: Data types are nothing but different types of data that can be stored. JavaScript provides two types of data types: Primitive and Non-primitive. It is important to note that JavaScript is a dynamically typed language. It means that we do not have to specify the data type for a variable, it is done dynamically by the JavaScript engine.   1. Primitive Data Types: Number : It represents numeric values, both with and without decimal. Eg: let x = 5;   String : It represents a sequence of characters inside quotes. It can consist of letters, words, and sentences.  It is important to note that even numbers enclosed inside quotes are treated as strings. Eg: let x = "Test123";   Boolean : It represents either true or false. Eg: let a = true;   Undefined : A variable that has not been assigned a value is of type undefined. Eg: let a;   null : It represents null, which means no value at all. Eg: let a = null;   2. Non-primitive dat...

else-if condition in if-else statements: Concatenation in JavaScript :

Image
  else-if condition in if-else statements: While specifying conditions for if-else statements, there may arise a need to include more than just two scenarios. For example, suppose we have a variable ‘n’ and we have to check if it is positive or negative. JS code for this problem would look like this: Quite simple, right? However, suppose we twist this problem a bit. If we have to check if the value of ‘n’ is positive, negative, OR zero. Can we do it using only if-else?  No, right? Here comes the scenario where we have to check more than two conditions and give output for each. This is where else-if comes into the game.  Structure of if else-if else looks something like this: If (condition 1)  { statements } else if (condition 2) { statements } else  { statements }   Working for the above code is explained as follows: Condition 1 is checked first. If it is true, statements inside the first block are executed and the remaining conditions are not checked. If t...

Modulo operator: Scope of let and const: What is a block?

Image
  Modulo operator: Modulo operator (%) is used to find the remainder when two numbers are divided. Example: Scope of let and const: One of the major differences between var and let/const is the scope. However, one might wonder what scope exactly is. The scope of a variable is the segment of the code where the variable can be accessed. Variables declared using var have the global scope whereas let and const are blocks scoped. Here, global scope means the value of a variable is accessible globally within the code. On the other hand, block scope means the value of the variable is accessible only within the block where it is declared.      What is a block?   A block is nothing but a set of statements enclosed inside curly braces. It is also called a Compound Statement. It is used whenever we want to execute several lines whereas JS allows only one. For example, we can use a block after an if condition to execute the block code conditionally.      Exam...

JavaScript as a scripting language:

  JavaScript as a scripting language: JavaScript (also known as JS) is a scripting language, but what does scripting language mean?  A scripting language is a programming language that allows some control of another software application(s). In the case of JavaScript, this application is the browser.  JavaScript is actually an interpreted language and not a compiled language.  The compilation is the process of converting high-level programming languages like Java, C++, etc. into machine code that a computer can execute. In the case of JavaScript, an interpreter in the browser interprets each line of JavaScript code and runs it. JavaScript can "display" data in different ways: Writing into an HTML element, using  innerHTML . Writing into the HTML output using  document.write() . Writing into an alert box, using  window.alert() . Writing into the browser console, using  console.log() .