Skip to main content

Command Palette

Search for a command to run...

Javascript Tips and Tricks

Javascript Tips and Tricks

Updated
3 min readView as Markdown
Javascript Tips and Tricks

In this post, we will be looking at some popular and must use Javascript Tips and tricks to boost your productivity and write clean code.

Optional Chaining

we usually get stuck in accessing nested properties in a javascript object, most of the time initial render for the content throws an error.

Ex:

   var array;
   array = fetchLogicWhichReturnsArray( )

   renderMethod(){
      array.map( data => {
           dispalyData(data);
      }

Most of the time you will get an array saying .map is not function, this is the most common error as a beginner in Javascript world, or even when your access a nested object data eg: data.userName it would throw an error like data.userName does not exist or is not defined.

These are the most common error, where as a Beginner i would write following code:

    if(data){
        if(data.userName){
              logicFunction()
        }
   }

There is nothing wrong with the above chunk of code, but this spoils the readability of code, in Javascript we can now use optional Chaining to prevent such stuff of code.

The code goes like this:

   const userName = data?.response?.allUsers?.user[0]?.userName

The above piece of code, get equivalent of nested conditional checks using if else, using optional chaining is counted in best practices in Javascript world.

Sum of values in an Array

javascript is no doubt a beautiful language, but Javascript is really powerful and also developer-friendly.

To add an Array, the simplest logic we would use is a for loop, and the worst-case complexity for it would be O(n) for sure.

    int Arr[ ] = {1, 2,3,4};  // any size is possible 
    int sum=0;
    for(int i=0; i<Arr.size(); i++){
        sum = sum +Arr[i];
    }
    cout<<sum;  // outputs: 10

in Javascript, we could do it simply

var Arr = [1, 2,3,4];
var sum = Arr.reduce((x, y) => x + y);
console.log(sum) // outputs 10;

The above code looks pretty for sure.

Resizing Array

Resizing an Array can be really hard in the world of Programming, in the world cpp we will have to create a new array and copy elements over to the new Array but Javascript got us.

To Resize an Array we can simply shorten the length of the array, using .length function proved with Array Templates

Let us see how:

var Arr =[3, 6, 8, 44, 99, 77, 33];

Arr.length = 4;
console.log(Arr.length) // outputs 4
console.log(Arr) // [3 ,6 ,8 ,44[

Arr.length =0
console.log(Arr) // output [ ]

### Get Unique Values of an Array

Getting an array of unique values is probably easier than you think in Javascript, wherein in other programming languages you have to sort the array, then iterate over to copy not duplicate values.

```js
var Arr = [...new Set([1, 44, 66, 88, 44, 99])]
console.log(Arr)  // outputs: [ 1, 44, 66, 88, 99 ]

Merge Objects

Merging Objects is a really tough task in most of the Programming languages, but javascript has all you need, to merge multiple objects, you can simply use the rest operator.

Eg:

const user ={ name:  'Jessica', gender : 'Female'}
const userName = { userName : 'jesss_codes'}
const profile = { role: 'Full Stack Developer'}

const AboutUser ={ ...user, ...userName, ...profile}

// AboutUser ={
// name:  'Jessica', gender : 'Female'
// userName : 'jesss_codes'
// role: 'Full Stack Developer'
// }

Using Regex

To change or remove values from a string in Javascript you can simply use an inbuilt Javascript function

eg:

var string = "Jessica"; //

 console.log(string.replace(/ss/, "sss"));  // outputs: Jesssica

Thanks a lot for giving it a Read.

Thumbnail from: freecodecamp.org/news/9-neat-javascript-tricks-e2742f2735c3/

N
Nate Sire5y ago

Optional chaining is essentially the equivalent of null object pattern. It's one reason chaining is forbidden in OOP... Law of Demeter.

J

Nice and knowledgeable article.

But I guess the markdown code is broken. image.png

D

".map is not function, this is the most common error as a beginner in Javascript worldJessica Carter

I have to respectfully disagree here;

If you try to call map on a null, you will get

cannot call map of undefined

The other common error is "

undefined is not a function

This when one attempts to call a non-scoped function or an undefined function.

Also. In my opinion, optional chaining is certainly a healthy feature for javascript, and it mirrors a similar approach in Typescript. However be wary of its overuse, sometimes an if-else is far cleaner and less obtuse than a long-chained optional object. I'd argue that optional chaining should be used for a limited amount of depth. If you need longer, then perhaps abstraction of one's code rather than a convoluted if-else, optional chain, or switch.

Also, (perhaps more pedantically on my part), the use of var is somewhat now no longer applicable, let and const are the accepted language constructs.

C

Great article! By the way, the Twitter link doesn't seem to work! 😁

H

Optional Chaining is a game changer.

Also a typo there you made with Declaring Array.

L

Good but beware mistypo in reduce example : {1, 2,3,4} instead of [1, 2,3,4]

1
J

Thanks, fixed it.

V

Hi Jessica, why use var (instead of let/const) curly braces (instead of square brackets) while declaring an array? I'm curious

3
J

Great Catch, Thanks a lot just fixed it.

G

Thanks for sharing the helpful content, As always.

All the Very best!

3
J

Thanks a lot❤️

T
Taiwo Awe5y ago

Very nicely written article. Your points were clearly articulated. Kudos Jessica

2
J

Thanks a lot ❤️

More from this blog

Full Stack Developer Blog

3 posts

Full stack Developer, Javascript fanGirl. All About Health 🍕 Life.