---
title: "Discussion on \"5 Neat JavaScript tricks for better programming.\" | Hashnode"
description: "Discussion on \"5 Neat JavaScript tricks for better programming.\". 1. Clearing or truncating an array - Change the length property.\nconst arr = [11, 22, 33, 44, 55, 66];\n// truncanting\narr.length = 3;\nconsole.log(arr); //=> [11, 22, 33]\n// clearing\narr.length = 0;\nconsole.log(arr); //=> []\nconsole.log(arr[2]); //=> ..."
url: "https://hashnode.com/posts/5-neat-javascript-tricks-for-better-programming/5be678f18b9c5a483696f885"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [5 Neat JavaScript tricks for better programming.](https://hashnode.com/post/5-neat-javascript-tricks-for-better-programming?utm_source=hashnode&utm_medium=feed)

1\. Clearing or truncating an array - Change the length property. const arr = \[11, 22, 33, 44, 55, 66\]; // truncanting arr.length = 3; console.log(arr); //=> \[11, 22, 33\] // clearing arr.length = 0; console.log(arr); //=> \[\] console.log(arr\[2\]); //=> ...

Nov 10, 2018 · hashnode.com  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#javascript-library](https://hashnode.com/tag/javascript-library)
-   [#javascript-modules](https://hashnode.com/tag/javascript-modules)
-   [#nodejs](https://hashnode.com/tag/nodejs)

## Written by

[Vimal Tiwari](https://hashnode.com/@vimaltiwari2612)

@vimaltiwari2612 · India

  
[View profile](https://hashnode.com/@vimaltiwari2612)

Software Engineer | Writer | Dreamer

Software Engineer with 4 years of experience in IT Industry. 3+ years of experience in Salesforce Application Development (Lightning / 1.5x Ranger TrailHead). 2x certified Salesforce (Platform Developer 1 &amp; 2). 2+ years of experience in Java &amp; Android development. 1+ years of experience in web development (jquery, Bootstrap, JavaScript). 1+ years of experience in Technical/Non-Technical Writing. Good command over Data Structures and Algorithms. Gate CS 2018 qualified.

## Responses 1

Markdown supported · ⌘↵ to post

[Jason Knight](https://hashnode.com/@cutcodedown)

[Nov 10, 2018](https://hashnode.com/posts/5-neat-javascript-tricks-for-better-programming/5be678f18b9c5a483696f885/comment/5be6fd1c273cedaf50dafbf8)

That "trick" of "switch(true)" is one of the most wasteful from a processing and memory standpoint there is. It entirely defeats the purpose of using switch (setting the value to compare against once) and results in doing MORE comparisons than necessary as you have a literal "true ===" instead of a soft conditional.

That is most certainly a case where if/else would be more efficient -- and really no more or less code.

Pretty much if you don't put the variable into your SWITCH, you're completely defeating the point of using it instead of if/else.

The example also has a wasted less than. It already tested for <=0 so why would you need to test for >0 afterwards? _The "variable for nothing" isn't helping matters either..._

Why would anyone use that instead of:

```
function getWaterState(tempInCelsius) {
    if (tempInCelsius <= 0) return 'Solid';
    if (tempInCelsius < 100) return 'Liquid';
    return 'Gas';
}
```

Or even better, go ternary given what's being done.

```
function getWaterState(tempInCelsius) {
    return tempInCelsius <= 0 ? 'Solid' : (
        tempInCelsius < 100 ? 'Liquid' : 'Gas'
    );
}
```

The whole point of switch/case is the efficiency of using a variable on the switch against a single value on the case, playing directly to how at the machine language level the various "Jxx" commands such as JZ (jump on zero), JC (jump on carry), JS (jump on sign) after a CMP (compare), ADD, SUB, or any other operation that sets the flags. This is particularly true of operations that can be fit into a single register (like integers) though string comparisons can 'short circuit' on mismatch using (on x86/x64) REP CMPSB _(if the lengths are known, gets more complex when they aren't -- another of the many reasons not to use null terminated strings no matter how much C loves them!)_.

Using it with a static value on the switch is just introducing extra overhead and processing time for zero real improvement in code clarity and certainly doesn't result in writing less code. I don't know who started telling people to use that technique as I've been seeing it for over a decade now, but they really needed to learn more about how things work at the low level before telling others how to code. Sadly like a great many coding practices (good or bad) one person shows it as a method, the majority just blindly parrots and copies it.

The title for #3 is also wrong, is it not? Shouldn't it be "**not** having a constructor or methods"?

[Vimal Tiwari](https://hashnode.com/@vimaltiwari2612)

[Nov 10, 2018](https://hashnode.com/posts/5-neat-javascript-tricks-for-better-programming/5be678f18b9c5a483696f885/comment/5be6fd1c273cedaf50dafbf8#reply-5be6fe60c2e0c6ee7ac193ff)

#2 is written for explaining the use of switch statement.

Moreover, the post is written for new tricks, not for efficient programming. But i appreciate your response. Thanks for such an explanatory feedback. It's quite useful.
