Tips and tricks that JavaScript programmers need to know
It is recommended to use 3 equal signs instead of two equal signs
Two equal signs (==) do not guarantee coherence. For example, it automatically converts the data type every time it is needed. Whereas triple equals sign (===) gives you a closer check for equality. Example: It checks both value and data type. Therefore, you should use three equal signs to compare two objects.
// là false, vì nó kiểm tra kiểu dữ liệu của cả LHS và RHS [1] === 1 // true, vì nó typecasts giá trị bên trái, là 1, bằng RHS. [1] == 1 // false, vì LHS là một ký tự còn RHS là số nguyên. '2' === 2 // true, do typecasting. '2' == 2 'undefined'=='undefined' // true 'undefined' === 'undefined' // true 0 == false // true 0 === false // false
Use SET to get unique values
In an array provided in JavaScript (ES6 version), if we are copying values inside it, and want to have all unique values, store this array in a specified SET object. introduced in version ES6.
// arr là array trùng lặp mảng số nguyên Const arr = [10,10,20,20,30]; = [10,10,20,20,30]; // Lư trữ mảng trên trong new_arr const new_arr = [.new Set(arr)]; // Print new_arr console.log(new_arr); // Result: [10, 20, 30]
Result:[10, 20, 30]
Without Set, this process will be time consuming. You have to iterate through the array and store the unique elements one after another in a new object.
Direct manipulation in Console.log()
You can directly perform operations on console.log() itself. You do not need to calculate before printing to the console. It can perform Add, Subtract, Multiply, Divide, Power, Increment, Decrement and logic operations on its panel.
var a = 10; var b = 20; // Phép cộng console.log("Addition of a and b: " + (a + b) ); // Phép trừ console.log("Subtraction: " + (a - b) ); // Phép nhân console.log("Multiplication between a and b: " + (a * b) ); // Phép chia console.log("The Quotient will be: " + (b / a) ); // Modulo, khi a chia cho b console.log((a % b)); // pre-increment console.log("Value of a after pre-increment: " + (++a) ); // post-increment console.log("Value of a after post-increment: " + (a++) ); // pre-decrement console.log("Value of b after pre-decrement: " + (--b) ); // post-decrement console.log("Value of b after post-decrement: " + (b--) ); // Lũy thừa console.log("2 raise to the power 3: " + (2**3);
Reduce the length of an array
You can reduce the length of an array using the function ( array.length).
Suppose, there is an array of size 10. And you need the first element k in it, (k < 10) of the array, You can reduce the length of the array to get the array of reduced size as you want.
For example:
Array: [ 11, 12, 122, 133, 152], size= 4;
Here we need an array of 2. So that array will be [ 11,12 ].
var array = [1, 2, 5, 78, 98] console.log("The array of size ",array.length) // kích thước của mảng là 4 array.length=4; console.log("The array of size ", array.length,"is :",array) // kích thước của mảng là 3 array.length=3; console.log("The array of size ", array.length,"is :",array) //kích thước của mảng là 2 array.length=2; console.log("The array of size ", array.length,"is :",array)
Use Slice to get selected elements in an array
The Slice method is used in arrays to get selected elements from an array. It returns a new array containing the selected elements, but the original array is left untouched.
Syntax:array.slice(start, end)
Above are some useful JavaScript programming tips for programmers. Hope the article is useful to you.
You should read it
- Syntax of JavaScript
- How to use Intl API in JavaScript
- Udemy's top 5 JavaScript courses
- What is JavaScript?
- What is the difference between Java and JavaScript?
- Tutorial for creating slideshows in JavaScript with 3 easy steps
- Learn about ES6 in Javascript
- Summary of JavaScript exercises with sample code
- What is Currying in Javascript? How to use Currying in JavaScript
- 9 popular JavaScript interview questions
- JavaScript code to generate error charts & graphs
- In the end, big universities realized that Java was a lousy language if used for introductory programming
Maybe you are interested
How to use the RANDARRAY function to randomly classify data in Excel
Write a program to find the majority element in an array in Python
Top 10 best array games for PC
Array formulas in Excel - Tutorials and examples
MODE.SNGL function - Function that returns the most frequently occurring, or the most repeated values in an array or data range in Excel
LINEST function - The function returns a line description array using the least square method in Excel