Dev Dastan
324 subscribers
49 photos
2 videos
41 links
πŸ”Έ Programming Tips and Concepts πŸ‘Ύ

πŸ”ΉContact: @Hossein13M 🀘🏻
Download Telegram
πŸ“± JavaScript Quiz

⁉️ What is the output of this code?

function Car() {
this.make = 'Lamborghini';
return { make: 'Maserati' };
}

const myCar = new Car();
console.log(myCar.make);


βœ”οΈ Answer:
When a constructor function is invoked with the new keyword, it creates an object and sets this to refer to that object. If the constructor function doesn't explicitly return anything, it defaults to returning the newly created object. In the case of the constructor function Car, it explicitly returns a new object with 'make' set to "Maserati", overriding the default behavior. Therefore, when new Car() is called, the returned object is assigned to myCar, resulting in the output "Maserati" when myCar.make is accessed.

βž–βž–βž–βž–βž–βž–
#JavaScript #Quiz


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
🀯10πŸ‘9❀4
πŸ“± Promise.all vs Promise.allSettled


πŸ•― Promise.all and Promise.allSettled are two methods provided by JavaScript's Promise API, but they have different behaviors and use cases.

πŸ”£ Promise.all
Waits for all promises to resolve or rejects immediately if any promise rejects. Resolves with an array of resolved values.


πŸ”£ Promise.allSettled
Waits for all promises to settle (resolve or reject) and always resolves. Returns an array of objects representing the outcomes of the promises, including their status and value/reason.


βž–βž–βž–βž–βž–βž–
πŸ“š Article: [here]
πŸ“± YouTube Video: [here]
πŸ“± Image Credit (on X.com): [here]
βž–βž–βž–βž–βž–βž–
#JavaScript


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
❀8πŸ‘3
πŸ“± JavaScript Quiz

⁉️ What's the value of num?

const num = parseInt('7*6', 10);


πŸ”€ 42
πŸ”€ "42"
πŸ”€ 7
πŸ”€ NaN

βœ”οΈ Answer:
parseInt returns only the first number in the string. Depending on the radix (the second argument specifying the number type: base 10, hexadecimal, octal, binary, etc.), parseInt validates the characters in the string. If it encounters an invalid character, it stops parsing and ignores the rest.

For example, in "78", only "7" is valid, so it parses it into the decimal 7, ignoring the "" and "8". Thus, num now equals 7.


βž–βž–βž–βž–βž–βž–
#JavaScript #Quiz


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘8🀯5
πŸ–ΌοΈ React State vs Refs


πŸ”£ State: For data that changes the UI. Updates trigger re-renders.

import { useState } from 'react';

function Counter() {
const [count, setCount] = useState(0);

return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}


πŸ”£ Refs: For directly accessing DOM elements or storing mutable values. Don't trigger re-renders.

import { useRef } from 'react';

function InputFocus() {
const inputRef = useRef(null);

function handleFocus() {
inputRef.current.focus();
}

return (
<div>
<input ref={inputRef} />
<button onClick={handleFocus}>Focus Input</button>
</div>
);
}


✨ Keep in mind that:

- State is immutable
- Avoid reading/writing refs during rendering
- Both persist data and have different update behaviors

βž–βž–βž–βž–βž–βž–
πŸ“š Article: [here]
βž–βž–βž–βž–βž–βž–
#JavaScript #ReactJS


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘11😎1
πŸ“± JavaScript Quiz

⁉️ What's the output?

const user = {
email: "[email protected]",
updateEmail: email => {
this.email = email;
}
}

user.updateEmail("[email protected]")
console.log(user.email)


πŸ”€ [email protected]
πŸ”€ [email protected]
πŸ”€ undefined
πŸ”€ ReferenceError

βœ”οΈ Answer:
The updateEmail function is an arrow function, and is not bound to the user object. This means that the this keyword is not referring to the user object, but refers to the global scope in this case. The value of email within the user object does not get updated. When logging the value of user.email, the original value of [email protected] gets returned.

βž–βž–βž–βž–βž–βž–
#JavaScript #Quiz


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘9🀯5❀4
πŸ“± JavaScript Quiz

⁉️ What's the output?

const box = { x: 10, y: 20 };

Object.freeze(box);

const shape = box;
shape.x = 100;

console.log(shape);


πŸ”€ { x: 100 , y: 20 }
πŸ”€ { x: 10, y: 20 }
πŸ”€ { x: 100 }
πŸ”€ ReferenceError

βœ”οΈ Answer: πŸ”€
Object.freeze prevents adding, removing, or modifying properties of an object (unless the property’s value is another object).

When we create the variable shape and set it equal to the frozen object box, shape also refers to a frozen object. You can check if an object is frozen using Object.isFrozen. In this case, Object.isFrozen(shape) returns true.

Since shape is frozen and x is not an object, we cannot modify x. Therefore, x remains 10, and { x: 10, y: 20 } gets logged.


βž–βž–βž–βž–βž–βž–
#JavaScript #Quiz


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘16😎2🀯1
πŸ“± JavaScript Quiz

⁉️ What is the event.target when clicking the button?

<div onclick="console.log('first div')">
<div onclick="console.log('second div')">
<button onclick="console.log('button')">
Click!
</button>
</div>
</div>


πŸ”€ Outer div
πŸ”€ Inner div
πŸ”€ button
πŸ”€ An array of all nested elements.

βœ”οΈ Answer: πŸ” 
The deepest nested element that caused the event is the target of the event. You can stop bubbling by event.stopPropagation


βž–βž–βž–βž–βž–βž–
#JavaScript #Quiz


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
🀯5πŸ‘4❀3
πŸ–Ό 45 JavaScript Super Hacks Every Developer Should Know


βœ” JavaScript is a versatile and powerful language that is essential for modern web development. Here are super hacks that will make you a more efficient and effective JavaScript developer, with detailed explanations and examples for each one.

πŸ”— Please open the link below to read them all!

βž–βž–βž–βž–βž–βž–
πŸ“š Article: [here]
βž–βž–βž–βž–βž–βž–
#JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘5
πŸ–ΌοΈ React is Just document.createElement() With Good Marketing, and I Can Prove It

βž– React, the darling of modern web development, is nothing more than document.createElement() wrapped in excellent marketing and increasingly complex abstractions.

βž–βž–βž–βž–βž–βž–
πŸ“š Article: [here]
βž–βž–βž–βž–βž–βž–
#javaScript #react


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
❀7πŸ‘Ž1
This media is not supported in your browser
VIEW IN TELEGRAM
πŸ–ΌοΈ The Hidden Cost of JavaScript Arrays (Part 1 - Memory Allocation)


βœ”οΈ JS arrays can tank performance and there are ways to optimize them.

❌ Operations like push(), pop(), and splice() can trigger memory reallocation, copying, and fragmentation.

βœ… Using fixed-size or oversized arrays can be more efficient.



// Inefficient: Repeatedly resizing the array

let arr = [];

for (let i = 0; i < 10000; i++) {
arr.push(i);
}

// Efficient: Using a pre-sized array

let arr2 = new Array(10000);


for (let i = 0; i < 10000; i++) {
arr2[i] = i;
}




βž–βž–βž–βž–βž–βž–
πŸ“š Article: [here]
βž–βž–βž–βž–βž–βž–
#javaScript #performance #softwareEngineering


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
❀3πŸ‘2
πŸ–ΌοΈ A Brief History of JavaScript: This year, JavaScript turns 30


πŸ”Έ 1994: Netscape releases Netscape Navigator 1.0


πŸ”Έ 1995: Brendan Eich creates the very first version of JavaScript


πŸ”Έ 1996: Microsoft introduces JScript in Internet Explorer 3 to compete with Netscape Navigator


πŸ”Έ 1996: Netscape Navigator 2.0 is released with JavaScript 1.0


πŸ”Έ 1997: Netscape submits JavaScript to ECMA International


πŸ”Έ 1998: Official release of the first ECMAScript language specification (ECMAScript 2)


πŸ”Έ 1999: Microsoft releases Internet Explorer 5, which uses more proprietary technology than before.


πŸ”Έ 2001: The first JSON message is sent


πŸ”Έ 2002: JSLint, the β€œgrandfather of all JavaScript syntax checkers” is introduced


πŸ”Έ 2003: Apple introduces Safari and WebKit


πŸ”Έ 2004: A beta version of Gmail is released, which uses a new asynchronous JavaScript protocol, β€œAJAX”


πŸ”Έ 2006: John Resig creates first commit to a project named jQuery


πŸ”Έ 2007: The first Apple iPhone is released with its mobile safari not supporting Flash


πŸ”Έ 2008: Netscape Navigator is sunset, marking the end of the β€œFirst Browser War”


πŸ”Έ 2008: Douglas Crockford publishes β€œJavaScript: The Good Parts”


πŸ”Έ 2008: Google releases the Chrome browser, the fastest web browser at the time, and with it, the V8 engine.


πŸ”Έ 2009: Ryan Dahl begins work on Node.js


πŸ”Έ 2009: The first commit on Express.js is created


πŸ”Έ 2010: npm 1.0 is released


πŸ”Έ 2010: WebStorm 1.0, a new JavaScript IDE by JetBrains, is released


πŸ”Έ 2010: AngularJS and Backbone.js are released


πŸ”Έ 2012: Webpack, a module bundler, is introduced


πŸ”Έ 2012: Microsoft makes TypeScript 0.8 available for the public


πŸ”Έ 2013: Facebook releases React


πŸ”Έ 2014: Vue.js is released


πŸ”Έ 2014: The first commit to Babel.js is created


πŸ”Έ 2014: Amazon announces AWS Lambda, powered by Node.js


πŸ”Έ 2015: GraphQL, a query language for APIs, is launched


πŸ”Έ 2015: Redux is released


πŸ”Έ 2015: Web assembly is released


πŸ”Έ 2015: ECMAScript 6 (ES2015) is released


πŸ”Έ 2016: Microsoft releases VSCode 1.0, a lightweight, fast, cross-platform IDE


πŸ”Έ 2016: Angular (Angular2) is released


πŸ”Έ 2016: Next.js 1.0 is released


πŸ”Έ 2017: Prettier 1.0 is released


πŸ”Έ 2017: Facebook launches Yarn, a new package manager


πŸ”Έ 2018: TensorFlow.js is released, bringing machine learning to the browser via WebGL or WebGPU without needing compute


πŸ”Έ 2019: Node.js stabilizes support for ECMAScript modules in v13.2.0


πŸ”Έ 2020: JavaScript makes it into space with SpaceX Dragon


πŸ”Έ 2020: Deno 1.0 is released


πŸ”Έ 2022: Internet Explorer 11 is retired


πŸ”Έ 2023: Bun 1.0 is released


πŸ”Έ 2025: TypeScript will be ported to Go


βž–βž–βž–βž–βž–βž–
πŸ“š Article: [here]
βž–βž–βž–βž–βž–βž–
#JavaScript


πŸ–₯ Follow @devDastan for more content.
Please open Telegram to view this post
VIEW IN TELEGRAM
❀13πŸ‘5😎1