{"id":136374,"date":"2024-07-11T13:03:53","date_gmt":"2024-07-11T13:03:53","guid":{"rendered":"https:\/\/www.sushilkumar.ind.in\/blog\/?p=136374"},"modified":"2024-07-12T04:40:25","modified_gmt":"2024-07-12T04:40:25","slug":"uber-nodejs-javascript-and-reactjs-interview-questions-answers","status":"publish","type":"post","link":"https:\/\/www.sushilkumar.ind.in\/blog\/uncategorized\/uber-nodejs-javascript-and-reactjs-interview-questions-answers\/","title":{"rendered":"Uber NodeJS JavaScript and ReactJS interview questions answers"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Deep Copy vs Shallow Copy<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Shallow Copy<\/strong><\/h3>\n\n\n\n<p>A shallow copy creates a new object with a new reference, but the properties of the new object refer to the same memory locations as the properties of the original object.<\/p>\n\n\n\n<p><strong>JavaScript Code Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const original = { a: 1, b: { c: 2 } };\nconst shallowCopy = { ...original };\nshallowCopy.b.c = 3;\n\nconsole.log(original.b.c);  \/\/ Output: 3 (Because the inner object is shared)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Deep Copy<\/strong><\/h3>\n\n\n\n<p>A deep copy creates a new object and recursively copies all objects and arrays, ensuring no references to the original object.<\/p>\n\n\n\n<p><strong>JavaScript Code Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const original = { a: 1, b: { c: 2 } };\nconst deepCopy = JSON.parse(JSON.stringify(original));\ndeepCopy.b.c = 3;\n\nconsole.log(original.b.c);  \/\/ Output: 2 (The inner object is not shared)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Deep Copy using a Custom Function<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function deepCopy(obj) {\n    return JSON.parse(JSON.stringify(obj));\n}\n\nconst original = { a: 1, b: { c: 2 } };\nconst copy = deepCopy(original);\ncopy.b.c = 3;\n\nconsole.log(original.b.c);  \/\/ Output: 2<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Search Key-Value Pairs in Nested Object with Recursions<\/strong><\/h2>\n\n\n\n<p><strong>JavaScript Code Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function findValue(obj, keyToFind) {\n    let result = null;\n\n    function search(obj) {\n        for (let key in obj) {\n            if (key === keyToFind) {\n                result = obj&#91;key];\n                return;\n            }\n            if (typeof obj&#91;key] === 'object' &amp;&amp; obj&#91;key] !== null) {\n                search(obj&#91;key]);\n            }\n        }\n    }\n\n    search(obj);\n    return result;\n}\n\nconst data = { a: { b: { c: 1 } } };\nconsole.log(findValue(data, 'c'));  \/\/ Output: 1<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is Currying Function?<\/strong><\/h2>\n\n\n\n<p>Currying is a technique where a function returns another function that accepts one argument at a time.<\/p>\n\n\n\n<p><strong>JavaScript Code Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function multiply(a) {\n    return function(b) {\n        return function(c) {\n            return a * b * c;\n        };\n    };\n}\n\nconsole.log(multiply(3)(2)(6));  \/\/ Output: 36<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Prototype Inheritance and Keeping Child Prototype Intact<\/strong><\/h2>\n\n\n\n<p><strong>JavaScript Code Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Parent() {\n    this.parentProperty = 'parent';\n}\n\nParent.prototype.parentMethod = function() {\n    console.log('Parent method');\n};\n\nfunction Child() {\n    Parent.call(this);\n    this.childProperty = 'child';\n}\n\n\/\/ Set up inheritance\nChild.prototype = Object.create(Parent.prototype);\nChild.prototype.constructor = Child;\n\nChild.prototype.childMethod = function() {\n    console.log('Child method');\n};\n\nconst child = new Child();\nchild.parentMethod();  \/\/ Output: 'Parent method'\nchild.childMethod();   \/\/ Output: 'Child method'<\/code><\/pre>\n\n\n\n<p>Using <code>Object.create<\/code> ensures that the <code>Child.prototype<\/code> object correctly inherits from <code>Parent.prototype<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Event Loop in Detail<\/strong><\/h2>\n\n\n\n<p>The Event Loop is a mechanism that allows JavaScript to perform non-blocking operations by using a single-threaded model. It handles callbacks, promises, and other asynchronous operations.<\/p>\n\n\n\n<p><strong>Steps of the Event Loop:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Execute Synchronous Code<\/strong>: Execute all the code that is present in the execution context.<\/li>\n\n\n\n<li><strong>Check the Callbacks Queue<\/strong>: After executing the code, the event loop checks the callback queue (task queue) to see if there are any functions to run.<\/li>\n\n\n\n<li><strong>Process the Callbacks<\/strong>: Process the functions from the callback queue.<\/li>\n\n\n\n<li><strong>Check for Microtasks<\/strong>: Before moving to the next event loop iteration, it processes microtasks (promises).<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Output Question Based on Event Loop<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(1);\n\nsetTimeout(() =&gt; { console.log(2) }, 0);\n\nPromise.resolve().then(() =&gt; console.log(3));\n\nPromise.resolve().then(() =&gt; setTimeout(() =&gt; { console.log(4) }, 0));\n\nPromise.resolve().then(() =&gt; console.log(5));\n\nsetTimeout(() =&gt; { console.log(6) }, 0);\n\nconsole.log(7);<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Synchronous Code<\/strong>: <code>console.log(1)<\/code> logs <code>1<\/code><\/li>\n\n\n\n<li><strong>Microtasks<\/strong>: <code>Promise.resolve().then(() =&gt; console.log(3))<\/code> logs <code>3<\/code><\/li>\n\n\n\n<li><strong>Microtasks<\/strong>: <code>Promise.resolve().then(() =&gt; console.log(5))<\/code> logs <code>5<\/code><\/li>\n\n\n\n<li><strong>Microtasks<\/strong>: <code>Promise.resolve().then(() =&gt; setTimeout(() =&gt; { console.log(4) }, 0))<\/code> is scheduled to run after the current microtasks<\/li>\n\n\n\n<li><strong>Timeouts<\/strong>: <code>setTimeout(() =&gt; { console.log(2) }, 0)<\/code> and <code>setTimeout(() =&gt; { console.log(6) }, 0)<\/code> are scheduled to run in the next event loop iteration<\/li>\n\n\n\n<li><strong>Synchronous Code<\/strong>: <code>console.log(7)<\/code> logs <code>7<\/code><\/li>\n\n\n\n<li><strong>Next Microtasks<\/strong>: Logs <code>4<\/code> from the <code>setTimeout<\/code> wrapped in a <code>Promise<\/code> resolved<\/li>\n\n\n\n<li><strong>Timeouts<\/strong>: Logs <code>2<\/code> and <code>6<\/code> from the <code>setTimeout<\/code> calls<\/li>\n<\/ol>\n\n\n\n<p><strong>Output Order:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\r\n7\r\n3\r\n5\r\n2\r\n6\r\n4<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>React<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Call Demo API and Filter List as We Type in Input Box<\/strong><\/h3>\n\n\n\n<p><strong>JavaScript Code Example (React):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, { useState, useEffect } from 'react';\nimport axios from 'axios';\n\nconst DemoComponent = () =&gt; {\n    const &#91;data, setData] = useState(&#91;]);\n    const &#91;query, setQuery] = useState('');\n    const &#91;filteredData, setFilteredData] = useState(&#91;]);\n\n    useEffect(() =&gt; {\n        async function fetchData() {\n            const result = await axios('https:\/\/api.example.com\/data');\n            setData(result.data);\n        }\n        fetchData();\n    }, &#91;]);\n\n    useEffect(() =&gt; {\n        setFilteredData(data.filter(item =&gt; item.name.toLowerCase().includes(query.toLowerCase())));\n    }, &#91;query, data]);\n\n    return (\n        &lt;div&gt;\n            &lt;input type=\"text\" value={query} onChange={e =&gt; setQuery(e.target.value)} \/&gt;\n            &lt;ul&gt;\n                {filteredData.map(item =&gt; (\n                    &lt;li key={item.id}&gt;{item.name}&lt;\/li&gt;\n                ))}\n            &lt;\/ul&gt;\n        &lt;\/div&gt;\n    );\n};\n\nexport default DemoComponent;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to Optimize React and Above React Project<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Use Memoization<\/strong>: Use <code>React.memo<\/code>, <code>useMemo<\/code>, and <code>useCallback<\/code> to prevent unnecessary re-renders.<\/li>\n\n\n\n<li><strong>Code Splitting<\/strong>: Dynamically import components to split code and improve load times.<\/li>\n\n\n\n<li><strong>Virtualization<\/strong>: Use libraries like <code>react-window<\/code> for rendering large lists efficiently.<\/li>\n\n\n\n<li><strong>Avoid Inline Functions<\/strong>: Move functions out of render methods to prevent creating new function instances.<\/li>\n\n\n\n<li><strong>Optimize Dependencies<\/strong>: Ensure that <code>useEffect<\/code> and <code>useCallback<\/code> dependencies are well managed to avoid excessive re-renders.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>JavaScript<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to Access DOM Elements in JS<\/strong><\/h3>\n\n\n\n<p><strong>JavaScript Code Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const elementById = document.getElementById('myId');\nconst elementByClassName = document.getElementsByClassName('myClass')&#91;0];\nconst elementByTagName = document.getElementsByTagName('div')&#91;0];\nconst elementByQuerySelector = document.querySelector('.myClass');\nconst elementsByQuerySelectorAll = document.querySelectorAll('.myClass');<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is Iframe<\/strong><\/h3>\n\n\n\n<p>An <code>&lt;iframe&gt;<\/code> element allows you to embed another HTML document within the current document.<\/p>\n\n\n\n<p><strong>Example HTML Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;iframe src=\"https:\/\/www.example.com\" width=\"600\" height=\"400\"&gt;&lt;\/iframe&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How to Access an Element from Iframe<\/strong><\/h3>\n\n\n\n<p><strong>JavaScript Code Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const iframe = document.getElementById('myIframe');\nconst iframeDocument = iframe.contentDocument || iframe.contentWindow.document;\nconst elementInIframe = iframeDocument.getElementById('elementId');<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is the Difference Between Window and Document<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Window<\/strong>: Represents the browser window or tab, and has properties like <code>alert<\/code>, <code>open<\/code>, and <code>location<\/code>.<\/li>\n\n\n\n<li><strong>Document<\/strong>: Represents the HTML content of the window and has properties like <code>getElementById<\/code>, <code>querySelector<\/code>, and <code>createElement<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What are Timers in JS<\/strong><\/h3>\n\n\n\n<p>Timers are functions that allow you to execute code after a delay.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>setTimeout(callback, delay)<\/code><\/strong>: Executes <code>callback<\/code> after <code>delay<\/code> milliseconds.<\/li>\n\n\n\n<li><strong><code>setInterval(callback, interval)<\/code><\/strong>: Repeatedly executes <code>callback<\/code> every <code>interval<\/code> milliseconds.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is Promise in JS<\/strong><\/h3>\n\n\n\n<p>A <code>Promise<\/code> represents a value that may be available now, or in the future, or never. It is a way to handle asynchronous operations.<\/p>\n\n\n\n<p><strong>JavaScript Code Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const promise = new Promise((resolve, reject) =&gt; {\n    \/\/ Asynchronous operation\n    if (true) {\n        resolve('Success');\n    } else {\n        reject('Error');\n    }\n});\n\npromise.then(result =&gt; console.log(result)).catch(error =&gt; console.log(error));<\/code><\/pre>\n\n\n\n<p>Certainly! Let\u2019s dive into the details of deep cloning limitations using <code>JSON.parse(JSON.stringify(obj))<\/code> and explore the concept of closures in JavaScript with an example.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Limitations of Deep Cloning Using <code>JSON.parse(JSON.stringify(obj))<\/code><\/strong><\/h2>\n\n\n\n<p>The <code>JSON.parse(JSON.stringify(obj))<\/code> method is a common approach for deep cloning objects in JavaScript, but it has several limitations:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">**1. *<em><code>Date<\/code> Objects are Lost<\/em>*:<\/h3>\n\n\n\n<p>When you use <code>JSON.stringify<\/code>, <code>Date<\/code> objects are converted to strings. The resulting <code>Date<\/code> information is lost after parsing.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const original = { date: new Date() };\nconst clone = JSON.parse(JSON.stringify(original));\n\nconsole.log(original.date);  \/\/ Output: Date object\nconsole.log(clone.date);    \/\/ Output: String representation of the Date object\nconsole.log(clone.date instanceof Date); \/\/ Output: false<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">**2. *<em><code>Functions<\/code> are Not Cloned<\/em>*:<\/h3>\n\n\n\n<p>Functions cannot be cloned using <code>JSON.stringify<\/code> because functions are not supported in JSON format.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const original = { func: () =&gt; console.log('Hello') };\nconst clone = JSON.parse(JSON.stringify(original));\n\nconsole.log(clone.func);  \/\/ Output: undefined<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">**3. *<em><code>undefined<\/code>, <code>Infinity<\/code>, and <code>NaN<\/code> Values are Ignored<\/em>*:<\/h3>\n\n\n\n<p>These values are not represented in JSON, so they are lost in the cloning process.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const original = { a: undefined, b: Infinity, c: NaN };\nconst clone = JSON.parse(JSON.stringify(original));\n\nconsole.log(clone.a);  \/\/ Output: undefined\nconsole.log(clone.b);  \/\/ Output: null (because Infinity is not represented in JSON)\nconsole.log(clone.c);  \/\/ Output: null (because NaN is not represented in JSON)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">**4. *<em><code>RegExp<\/code> Objects are Lost<\/em>*:<\/h3>\n\n\n\n<p>Regular expressions are not preserved through <code>JSON.stringify<\/code>.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const original = { regex: \/test\/ };\nconst clone = JSON.parse(JSON.stringify(original));\n\nconsole.log(clone.regex);  \/\/ Output: undefined\nconsole.log(clone.regex instanceof RegExp); \/\/ Output: false<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">**5. *<em><code>Symbol<\/code> Properties are Ignored<\/em>*:<\/h3>\n\n\n\n<p>Properties keyed by <code>Symbol<\/code> are not included in the JSON representation.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const sym = Symbol('mySymbol');\nconst original = { &#91;sym]: 'value' };\nconst clone = JSON.parse(JSON.stringify(original));\n\nconsole.log(clone&#91;sym]);  \/\/ Output: undefined<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">**6. *<em>Circular References are Not Supported<\/em>*:<\/h3>\n\n\n\n<p><code>JSON.stringify<\/code> cannot handle objects with circular references.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const a = {};\na.b = a;\n\ntry {\n    const clone = JSON.parse(JSON.stringify(a));\n} catch (error) {\n    console.error(error);  \/\/ Output: TypeError: Converting circular structure to JSON\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">**7. *<em>Prototype Chain is Not Cloned<\/em>*:<\/h3>\n\n\n\n<p>Only the properties of the object are cloned, not its prototype chain.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function MyClass() {\n    this.name = 'example';\n}\nMyClass.prototype.greet = function() { return 'Hello'; };\n\nconst original = new MyClass();\nconst clone = JSON.parse(JSON.stringify(original));\n\nconsole.log(clone.greet);  \/\/ Output: undefined<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the Usage of Closure? Show an Example<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is a Closure?<\/strong><\/h3>\n\n\n\n<p>A closure is a feature in JavaScript where an inner function retains access to the variables of its outer function even after the outer function has finished executing. This allows for the creation of private variables and functions, and it supports data encapsulation and function factories.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Usage of Closures<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Data Encapsulation<\/strong>: Protect data from being accessed directly from outside the function.<\/li>\n\n\n\n<li><strong>Function Factories<\/strong>: Create functions with specific configurations.<\/li>\n\n\n\n<li><strong>Maintaining State<\/strong>: Keep track of state across function calls.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example of a Closure<\/strong><\/h3>\n\n\n\n<p><strong>1. Data Encapsulation Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function createCounter() {\n    let count = 0;  \/\/ `count` is a private variable\n    return function() {\n        count++;\n        return count;\n    };\n}\n\nconst counter = createCounter();\nconsole.log(counter());  \/\/ Output: 1\nconsole.log(counter());  \/\/ Output: 2\nconsole.log(counter());  \/\/ Output: 3<\/code><\/pre>\n\n\n\n<p>In this example, <code>count<\/code> is a private variable that is only accessible through the returned inner function.<\/p>\n\n\n\n<p><strong>2. Function Factory Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function multiplyBy(factor) {\n    return function(num) {\n        return num * factor;\n    };\n}\n\nconst double = multiplyBy(2);\nconsole.log(double(5));  \/\/ Output: 10\n\nconst triple = multiplyBy(3);\nconsole.log(triple(5));  \/\/ Output: 15<\/code><\/pre>\n\n\n\n<p>Here, <code>multiplyBy<\/code> is a function factory that creates functions for multiplying numbers by a specific factor.<\/p>\n\n\n\n<p><strong>3. Maintaining State Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function createCounter(initialValue) {\n    let count = initialValue;\n    return {\n        increment() {\n            count++;\n            return count;\n        },\n        decrement() {\n            count--;\n            return count;\n        },\n        getValue() {\n            return count;\n        }\n    };\n}\n\nconst myCounter = createCounter(10);\nconsole.log(myCounter.getValue());  \/\/ Output: 10\nconsole.log(myCounter.increment()); \/\/ Output: 11\nconsole.log(myCounter.decrement()); \/\/ Output: 10<\/code><\/pre>\n\n\n\n<p>In this example, <code>myCounter<\/code> maintains the state of the count variable through multiple function calls.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Summary Table of Closures<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Feature<\/strong><\/th><th><strong>Description<\/strong><\/th><th><strong>Example<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>Data Encapsulation<\/strong><\/td><td>Hide private data from the outside world<\/td><td><code>const counter = createCounter();<\/code><\/td><\/tr><tr><td><strong>Function Factory<\/strong><\/td><td>Generate functions with customized behavior<\/td><td><code>const double = multiplyBy(2);<\/code><\/td><\/tr><tr><td><strong>Maintaining State<\/strong><\/td><td>Preserve state between function calls<\/td><td><code>const myCounter = createCounter(10);<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Deep Copy vs Shallow Copy Shallow Copy A shallow copy creates a new object with a new reference, but the properties of the new object refer to the same memory locations as the properties of the original object. JavaScript Code Example: const original = { a: 1, b: { c: 2 } }; const shallowCopy &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-136374","post","type-post","status-publish","format-standard","","category-uncategorized"],"jetpack_publicize_connections":[],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p99pkJ-ztA","_links":{"self":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136374"}],"collection":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/comments?post=136374"}],"version-history":[{"count":3,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136374\/revisions"}],"predecessor-version":[{"id":136377,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136374\/revisions\/136377"}],"wp:attachment":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media?parent=136374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/categories?post=136374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/tags?post=136374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}