This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fetch('https://api.ipify.org/?format=json') | |
| .then(results => results.json()) | |
| .then(console.log) // e.g. { ip: "113.114.194.164" } | |
| .catch(console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const searchParams = new URLSearchParams(window.location.search); | |
| const decodedParams = Array | |
| .from(searchParams.keys()) | |
| .reduce((acc, key) => ({ ...acc, [key]: searchParams.get(key) }), {}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // [{_id:"someid", name:"someUserName", age:21, countPosts: 15}]; | |
| db.findOne({ _id }) | |
| .then(results => ({ id: results._id, username: results.name })) | |
| .then(results => res.json(results)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Simple Node.js Echo Server that listens for data and sends a response with the data back to the client | |
| // Note: Responds to any verb eg. GET/POST etc. | |
| const createServer = require('http').createServer; | |
| const server = createServer((req, res) => { | |
| let body = ''; | |
| req.on('data', data => body += data) | |
| req.on('end', () => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <input id="inputText" value="Text to copy"> | |
| <button id="copyInputText">Copy</button> | |
| <span id="elementText">More Text to copy</span> | |
| <button id="copyElementText">Copy</button> | |
| <script> | |
| const execCopy = () => { | |
| inputText.select(); | |
| document.execCommand('copy') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const today = new Date(); | |
| const dd = String(today.getDate()).padStart(2, '0'); | |
| const mm = String(today.getMonth() + 1).padStart(2, '0'); | |
| const yyyy = today.getFullYear(); | |
| const todayDate = `${dd}/${mm}/${yyyy}`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const encodeHTML = str => str.replace(/[\u00A0-\u9999<>\&]/gim, (i) => `&#${i.charCodeAt(0)};`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Really simple validation | |
| const simpleEmailRegex = /\S+@\S+\.\S+/; | |
| simpleEmailRegex.test('[email protected]'); // true | |
| // Complex validation | |
| const complexEmailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
| complexEmailRegex.test('[email protected]'); // true | |
| // Source: https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // <div id="block" style="position: absolute; top: 0; left: 0; width: 100px; height: 100px; background-color: red;"></div> | |
| <script> | |
| let modifier = 5; | |
| window.addEventListener('keydown', (event) => { | |
| const { style } = block; | |
| switch(event.key) { | |
| case 'ArrowUp': style.top = `${parseInt(style.top) - modifier}px`; break; | |
| case 'ArrowDown': style.top = `${parseInt(style.top) + modifier}px`; break; | |
| case 'ArrowLeft': style.left = `${parseInt(style.left) - modifier}px`; break; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
| <title>Hover with JavaScript</title> | |
| <style> | |
| #hoverlink { | |
| padding: 5px; |