Enhancing JavaScript Code Readability with Template Literals
Best practices for using template literals to create more readable and maintainable string interpolations in JavaScript.
0 likes
9 views
Rule Content
--- name: Enhancing JavaScript Code Readability with Template Literals version: "1.0" category: JavaScript Cursor Rules description: Best practices for using template literals to create more readable and maintainable string interpolations in JavaScript. --- ## Rule: Prefer Template Literals for String Interpolation **Description:** Use template literals (backticks) instead of string concatenation for combining variables and expressions within strings to enhance readability and maintainability. **Example:** // Avoid const name = "Alice"; const age = 30; const message = "My name is " + name + " and I am " + age + " years old."; // Prefer const message = `My name is ${name} and I am ${age} years old.`; **Rationale:** Template literals provide a cleaner syntax for string interpolation, reducing the need for cumbersome concatenation and improving code clarity. ([codelucky.com](https://codelucky.com/javascript-best-practices/?utm_source=openai)) ## Rule: Utilize Template Literals for Multi-line Strings **Description:** Employ template literals for multi-line strings to avoid the use of newline characters (`\n`) and string concatenation. **Example:** // Avoid const message = "Hello,\n" + "Welcome to our website.\n" + "How can we assist you today?"; // Prefer const message = `Hello, Welcome to our website. How can we assist you today?`; **Rationale:** Template literals inherently support multi-line strings, making the code more readable and easier to maintain. ([freecodecamp.org](https://www.freecodecamp.org/news/template-literals-in-javascript?utm_source=openai)) ## Rule: Keep Expressions Simple Within Template Literals **Description:** Limit the complexity of expressions inside template literals to maintain readability. **Example:** // Avoid const total = `The total is ${items.map(item => item.price * item.quantity).reduce((sum, value) => sum + value, 0)}.`; // Prefer const itemTotals = items.map(item => item.price * item.quantity); const grandTotal = itemTotals.reduce((sum, value) => sum + value, 0); const total = `The total is ${grandTotal}.`; **Rationale:** Simplifying expressions within template literals enhances code readability and maintainability. ([medium.com](https://medium.com/%40francesco.saviano87/mastering-javascript-template-literals-b2d7cdf90a91?utm_source=openai)) ## Rule: Sanitize User Inputs in Template Literals **Description:** Always sanitize user inputs before embedding them in template literals to prevent security vulnerabilities. **Example:** // Avoid const userInput = '<script>alert("XSS Attack")</script>'; const message = `User input: ${userInput}`; // Prefer function escapeHTML(str) { return str.replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') .replace(/'/g, '''); } const safeInput = escapeHTML(userInput); const message = `User input: ${safeInput}`; **Rationale:** Sanitizing user inputs prevents potential security risks such as Cross-Site Scripting (XSS) attacks. ([medium.com](https://medium.com/%40francesco.saviano87/mastering-javascript-template-literals-b2d7cdf90a91?utm_source=openai)) ## Rule: Use Tagged Templates for Specialized String Processing **Description:** Implement tagged templates when custom processing of template literals is required, such as localization or input sanitization. **Example:** function sanitize(strings, ...values) { return strings.reduce((result, string, i) => result + string + (values[i] ? escapeHTML(values[i]) : ''), ''); } const userInput = '<script>alert("XSS Attack")</script>'; const message = sanitize`User input: ${userInput}`; **Rationale:** Tagged templates provide a powerful mechanism for custom string processing, enhancing flexibility and security. ([medium.com](https://medium.com/%40francesco.saviano87/mastering-javascript-template-literals-b2d7cdf90a91?utm_source=openai))