avatarSarah

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2366

Abstract

unction_">slice</span>(-<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>];</pre></div><h2 id="5421">05. Creating unique arrays</h2><p id="d9b0">Remove duplicate values from an array by converting it to a Set and back.</p><div id="afe9"><pre><span class="hljs-keyword">const</span> uniqueArray = [...<span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>(originalArray)];</pre></div><h2 id="4133">06. String reversal</h2><p id="0dd5">Reverse a string using the split(), reverse(), and join() methods.</p><div id="76e1"><pre><span class="hljs-keyword">const</span> reversedString = <span class="hljs-string">'Hello'</span>.<span class="hljs-title function_">split</span>(<span class="hljs-string">''</span>).<span class="hljs-title function_">reverse</span>().<span class="hljs-title function_">join</span>(<span class="hljs-string">''</span>);</pre></div><h2 id="c881">07. Fading elements</h2><p id="38a2">Create a smooth fade effect using requestAnimationFrame().Call fadeIn(element, duration) to gradually fade in an element.</p><div id="bd9f"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">fadeIn</span>(<span class="hljs-params">element, duration</span>) { <span class="hljs-keyword">let</span> start = <span class="hljs-literal">null</span>; <span class="hljs-keyword">function</span> <span class="hljs-title function_">animate</span>(<span class="hljs-params">timestamp</span>) { <span class="hljs-keyword">if</span> (!start) start = timestamp; <span class="hljs-keyword">const</span> progress = timestamp - start; element.<span class="hljs-property">style</span>.<span class="hljs-property">opacity</span> = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">min</span>(progress / duration, <span class="hljs-number">1</span>); <span class="hljs-keyword">if</span> (progress < duration) { <span class="hljs-title function_">requestAnimationFrame</span>(animate); } } <span class="hljs-title function_">requestAnimationFrame</span>(animate); }</pre></div><h2 id="9ec0">08. Intersection observer for lazy loading</h2><p id="9ecd">Use the Intersection Observer API for lazy loading images or triggering animations when elements come into view.</p><div id="f95c"><pre><span class="

Options

hljs-keyword">const</span> observer = <span class="hljs-keyword">new</span> <span class="hljs-title class_">IntersectionObserver</span>(<span class="hljs-function"><span class="hljs-params">entries</span> =></span> { entries.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">entry</span> =></span> { <span class="hljs-keyword">if</span> (entry.<span class="hljs-property">isIntersecting</span>) { <span class="hljs-comment">// Trigger your action</span> entry.<span class="hljs-property">target</span>.<span class="hljs-property">classList</span>.<span class="hljs-title function_">add</span>(<span class="hljs-string">'animate'</span>); } }); });

<span class="hljs-comment">// Start observing a target element</span> observer.<span class="hljs-title function_">observe</span>(targetElement);</pre></div><h2 id="2e82">09. async and await for asynchronous code</h2><p id="aba6">Simplify asynchronous code using the async and await keywords.</p><div id="f96f"><pre><span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">fetchData</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> <span class="hljs-title function_">fetch</span>(<span class="hljs-string">'https://api.example.com/data'</span>); <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> response.<span class="hljs-title function_">json</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data); }</pre></div><h2 id="53c3">10. Array.reduce() for aggregation</h2><p id="0365">Use Array.reduce() to aggregate values from an array.</p><div id="00ac"><pre><span class="hljs-keyword">const</span> numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]; <span class="hljs-keyword">const</span> sum = numbers.<span class="hljs-title function_">reduce</span>(<span class="hljs-function">(<span class="hljs-params">acc, curr</span>) =></span> acc + curr, <span class="hljs-number">0</span>);</pre></div></article></body>

10 lesser known JavaScript tricks part 2

Hello again coding friends! I’ve compiled a list of 10 awesome tricks to help you get the most out of your coding. Whether you’re just getting started or you’re already in the game, these tips will give your coding projects a boost.

01. The void operator

Use the void operator to evaluate an expression and return undefined.

const result = void function() {
    // Some code here
}();

02. Dynamic property names

Create dynamic object properties using square bracket notation.

const dynamicPropertyName = 'color';
const myObject = {
    [dynamicPropertyName]: 'blue'
};

03. Using Promise.allSettled()

Handle multiple promises with Promise.allSettled(), which returns an array of promise states regardless of fulfillment or rejection.

const promises = [promise1, promise2, promise3];
Promise.allSettled(promises)
    .then(results => console.log(results));

04. Negative indexing in arrays

Access array elements from the end using negative indexing.

const array = [1, 2, 3, 4, 5];
const lastElement = array.slice(-1)[0];

05. Creating unique arrays

Remove duplicate values from an array by converting it to a Set and back.

const uniqueArray = [...new Set(originalArray)];

06. String reversal

Reverse a string using the split(), reverse(), and join() methods.

const reversedString = 'Hello'.split('').reverse().join('');

07. Fading elements

Create a smooth fade effect using requestAnimationFrame().Call fadeIn(element, duration) to gradually fade in an element.

function fadeIn(element, duration) {
    let start = null;
    function animate(timestamp) {
        if (!start) start = timestamp;
        const progress = timestamp - start;
        element.style.opacity = Math.min(progress / duration, 1);
        if (progress < duration) {
            requestAnimationFrame(animate);
        }
    }
    requestAnimationFrame(animate);
}

08. Intersection observer for lazy loading

Use the Intersection Observer API for lazy loading images or triggering animations when elements come into view.

const observer = new IntersectionObserver(entries => {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            // Trigger your action
            entry.target.classList.add('animate');
        }
    });
});

// Start observing a target element
observer.observe(targetElement);

09. async and await for asynchronous code

Simplify asynchronous code using the async and await keywords.

async function fetchData() {
    const response = await fetch('https://api.example.com/data');
    const data = await response.json();
    console.log(data);
}

10. Array.reduce() for aggregation

Use Array.reduce() to aggregate values from an array.

const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
Js
JavaScript
Javascript Tips
Javascript Development
Front End Development
Recommended from ReadMedium