JavaScript is a scripting language that enables you to create dynamically updating content, control multimedia, animate images, and pretty much everything else.

To help everyone handle things more simply in JavaScript, I would like to introduce some tips and tricks that I have learned.

1. Every and some function:

every function returns a boolean value. If all elements in the array satisfy the condition, it returns true. Besides, some function only checks if there exists at least one element in the array that meets the condition and returns true.

     const numberArray1 = [2, 4, 6, 8, 10];

     const numberArray2 = [2, 4, 5, 8, 10];

     const isEvenNumber = (number) => {

          return number % 2 === 0;


     console.log(numberArray1.every(isEvenNumber)); // return true (because all elements in this array satisfy the condition)

     console.log(numberArray2.every(isEvenNumber)); // return false (because there is an element 5 that does not satisfy the condition)

     console.log(numberArray1.some(isEvenNumber)); // return true (there exists an element that satisfies the condition)

     console.log(numberArray2.some(isEvenNumber)) // return true (there exists an element that satisfies the condition)


2. Convert from Number to Boolean:

Declaring a variable with a boolean value, in addition to the usual declaration (giving the value of the variable true or false), can be done in the following way.

     const isTrue = !0;

     const isFalse = !1;

     const alsoFalse = !!0;

     console.log(isTrue); // return true

     console.log(isFalse); // return false

     console.log(alsoFalse); // return false


3. Convert to String type:

With other programming languages, we can convert numbers to strings using the toString() function. Although Javascript is the same, there is another faster way to do it.

     const string = 1 + ''; 

     console.log(string); // return '1'


4. Convert to Number type:

In Javascript, there are many ways to convert from String to Number. For example, parseInt(),... But there is a shorter way to do this, which is to use the ( + ) operator.

     const value = '10';

     value = +value;

     console.log(value); // return 10

     console.log(typeof value); // return 'number'


5. Convert from Decimal to Integer:

If you want to convert from a decimal number to an integer, you can use Object Math functions like Math.floor(), Math.ceil() or Math.round(). But the fastest way to do this is to use the bitwise OR ( | ) operator.

     console.log(10.9 | 0); // return 10

     console.log(10.1 | 0); // return 10


6. Use splice() instead of delete:

When using the delete method, an application replaces an item with undefined instead of removing it from the array. So it is better to use splice() to remove an item from an array.

When using delete:

    const fruits = ['apple', 'orange', 'kiwi', 'banana', 'lemon'];

     fruits.length; // return 5

     delete fruits[3]; // return true

     fruits.length; // return 5

     console.log(fruits); // ['apple', 'orange', 'kiwi', undefined, 'lemon']

When using splice():

     const fruits = ['apple', 'orange', 'kiwi', 'banana', 'lemon'];

     fruits.length; // return 5

     fruits.splice(3, 1); // return ['banana']

     fruits.length; // return 4

     console.log(fruits); // ['apple', 'orange', 'kiwi', 'lemon']


7. Combine two Objects using the spread operator:

There are many situations where you have to combine two or more data sets from different sources. In such cases, there are ways to do this in JavaScript.

Below is a typical example:

     const person = {id: '001', name: 'Tran Van A'};

     const address = {id: '001', add: 'district 1'};

     const personDetail = Object.assign({}, person, address);

     console.log(personDetail); // return { id: '001', name: 'Tran Van A', add: 'district 1'}

However, you can use the spread operator to combine person and address into a single object.

     const personDetail = { …person, …address };

     console.log(personDetail); // return { id: '001', name: 'Tran Van A', add: 'district 1'}

An important note is that duplicate keywords will be overwritten on previous objects.



Like any other programming language, JavaScript has many tips and tricks to handle Objects, Arrays, etc., allowing us to write our programs simpler and more beautifully without using functions, and verbose methods. Hope it will be of some help to those of you who are learning Javascript.




Leave a comment