DEV Community

Cover image for JavaScript's Best-Kept Secrets: Unveiling Lesser-Known Features!
PRINCE KUKREJA
PRINCE KUKREJA

Posted on

JavaScript's Best-Kept Secrets: Unveiling Lesser-Known Features!

JavaScript is a super cool language with lots of neat stuff, but guess what? JavaScript has some cool secret tricks that can make coding even more fun. Let me show you these hidden gems and how they can make your code better and your projects awesome!

  • Object.freeze()

The Object.freeze() method allows you to freeze an object, preventing any changes to its properties or prototype. This can be useful to ensure that an object remains immutable and its properties cannot be modified accidentally.

const person = {
  name: 'John',
  age: 30
};


Object.freeze(person);

person.age = 40; // This will not have any effect
console.log(person); // Output: { name: 'John', age: 30 }

Enter fullscreen mode Exit fullscreen mode
  • String.padStart() and String.padEnd()

The String.padStart() and String.padEnd() methods pad the current string with another string (repeated, if needed) so that the resulting string reaches the given length. These methods are useful for formatting strings, especially for aligning text in tables or UI elements.

const number = '123';
console.log(number.padStart(5, '0')); // Output: 00123

const message = 'Hello';
console.log(message.padEnd(10, '!')); // Output: Hello!!!!!
;
Enter fullscreen mode Exit fullscreen mode

These methods provide a convenient way to format strings.

  • Array.prototype.flat()

The Array.prototype.flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. This can be handy for flattening arrays with nested arrays.

const nestedArray = [1, [2, [3, [4]]]];
const flattenedArray = nestedArray.flat(Infinity);
console.log(flattenedArray); // Output: [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode
  • String.prototype.trimStart() and String.prototype.trimEnd()

These methods trim whitespace from the beginning (trimStart()) or end (trimEnd()) of a string. They are handy alternatives to String.prototype.trim() when you want to trim whitespace from specific sides of a string.

const text = '   Hello, World!   ';

console.log(text.trimStart()); // Output: 'Hello, World!   '
console.log(text.trimEnd());   // Output: '   Hello, World!'
Enter fullscreen mode Exit fullscreen mode
  • Object.fromEntries()

The Object.fromEntries() method transforms a list of key-value pairs into an object. It is the counterpart of Object.entries(), which returns an array of key-value pairs from an object.

const entries = [['name', 'John'], ['age', 30]];
const person = Object.fromEntries(entries);
console.log(person); // Output: { name: 'John', age: 30 }
Enter fullscreen mode Exit fullscreen mode

Conclusion
JavaScript has some really cool secrets that can make your code shorter, easier to understand and work better. Knowing and using these special features can make you a better JavaScript coder and make your projects look awesome.

So, next time you're coding in JavaScript, remember to explore these hidden treasures!

I hope you found these JavaScript tricks helpful! Feel free to share any more cool JavaScript tips you know in the comments below.
Happy coding!

Top comments (0)