Premium
JAVASCRIPT Snippets.

Curated list of production-ready JAVASCRIPT scripts and coding solutions.

JAVASCRIPT

Implementing CSRF Protection in Express.js with `csurf`

Secure your Express.js web forms and API endpoints against Cross-Site Request Forgery (CSRF) attacks using the `csurf` middleware and synchronizer tokens.

View Snippet →
JAVASCRIPT

Secure Cookie Configuration for Express.js Applications

Configure HTTP-only, secure, and SameSite attributes for cookies in Express.js to enhance application security and mitigate risks like XSS and CSRF.

View Snippet →
JAVASCRIPT

Implementing OAuth 2.0 Client Credentials Flow (Node.js)

Securely obtain an access token for server-to-server API authentication using the OAuth 2.0 Client Credentials Grant Type in Node.js with `axios`.

View Snippet →
JAVASCRIPT

Securely Uploading Files to an API with FormData (JavaScript Frontend)

Learn how to use JavaScript `FormData` to construct and send multipart/form-data requests, securely uploading files and additional data to an API.

View Snippet →
JAVASCRIPT

Real-time Data with WebSocket API Client (JavaScript)

Connect to a WebSocket API in JavaScript to enable real-time, bi-directional communication, sending messages and handling live data updates efficiently.

View Snippet →
JAVASCRIPT

React Hook: useClickOutside for Modals and Dropdowns

A custom React hook, `useClickOutside`, detects clicks outside a specified DOM element, useful for closing modals, dropdowns, or popovers when users click away.

View Snippet →
JAVASCRIPT

React Hook: useMediaQuery for Responsive Components

The `useMediaQuery` hook allows React components to respond to CSS media queries, enabling dynamic rendering or styling based on screen size or device features.

View Snippet →
JAVASCRIPT

React Hook: usePrevious for Tracking Value History

The `usePrevious` custom React hook provides a way to access the previous value of a state or prop, which is invaluable for comparing current and past states.

View Snippet →
JAVASCRIPT

React Hook: useLocalStorage for Persisting State

A `useLocalStorage` custom hook for React that automatically synchronizes component state with the browser's localStorage, ensuring data persistence across sessions.

View Snippet →
JAVASCRIPT

React Hook: useReducer for Complex State Logic

Leverage `useReducer` in React for managing complex state transitions, providing a robust and scalable alternative to `useState` for intricate component state.

View Snippet →
JAVASCRIPT

Validate Strong Password Policy (Min 8 Chars, Mixed Case, Digit, Special)

Implement a strong password validation using a single JavaScript regex to enforce minimum length, uppercase, lowercase, numbers, and special characters.

View Snippet →
JAVASCRIPT

Sanitize Input: Allow Only Alphanumeric, Spaces, and Basic Punctuation

Clean user input by removing all characters except alphanumeric ones, spaces, and common punctuation marks using a simple JavaScript regex replacement.

View Snippet →