Skip to main content

Sequential Queue

0 likes • Feb 6, 2021 • 2 views
JavaScript
Loading...

More JavaScript Posts

Bitwise XOR operator

0 likes • Nov 19, 2022 • 0 views
JavaScript
//JavaScript program to swap two variables
//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
// XOR operator
a = a ^ b
b = a ^ b
a = a ^ b
console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);

Convert data to yyyy mm dd format

0 likes • Nov 19, 2022 • 1 view
JavaScript
// `date` is a `Date` object
const formatYmd = date => date.toISOString().slice(0, 10);
// Example
formatYmd(new Date()); // 2020-05-06

Clone an array

0 likes • Nov 19, 2022 • 0 views
JavaScript
// `arr` is an array
const clone = arr => arr.slice(0);
// Or
const clone = arr => [...arr];
// Or
const clone = arr => Array.from(arr);
// Or
const clone = arr => arr.map(x => x);
// Or
const clone = arr => JSON.parse(JSON.stringify(arr));
// Or
const clone = arr => arr.concat([]);

localStorage Cookie Consent

0 likes • Oct 9, 2023 • 103 views
JavaScript
import React, { useState, useEffect } from 'react';
import Link from 'next/link';
export default function CookieBanner() {
// Initialize with a default value (false if not previously set in localStorage)
const [cookieConsent, setCookieConsent] = useState(() =>
localStorage.getItem('cookieConsent') === 'true' ? true : false
);
useEffect(() => {
const newCookieConsent = cookieConsent ? 'granted' : 'denied';
window.gtag('consent', 'update', {
analytics_storage: newCookieConsent
});
localStorage.setItem('cookieConsent', String(cookieConsent));
}, [cookieConsent]);
return !cookieConsent && (
<div>
<div>
<p>
We use cookies to enhance the user experience.{' '}
<Link href='/privacy/'>View privacy policy</Link>
</p>
</div>
<div>
<button type="button" onClick={() => setCookieConsent(false)}>Decline</button>
<button type="button" onClick={() => setCookieConsent(true)}>Allow</button>
</div>
</div>
)
}

CodeCatchCrasher

0 likes • Feb 11, 2021 • 2 views
JavaScript
function spam(times = 1,log = true){
for(let i = 0; i < times; i++){
$.get("backend-search.php", {term: (" "+Date.now())}).done(function(data){
if(log) console.log(data);
});
}
}
spam(100000);

Reverse a string

0 likes • Nov 19, 2022 • 1 view
JavaScript
const reverse = str => str.split('').reverse().join('');
reverse('hello world');
// Result: 'dlrow olleh'