Tips and tricks that JavaScript programmers need to know
JavaScript is the most widely used programming language today. If you are also using JavaScript to program a web, application or program, the following tips may help you.
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've just finished reading the article "Tips and tricks that JavaScript programmers need to know" edited by the TipsMake team. You can save tips-and-tricks-that-javascript-programmers-need-to-know.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.
- Tips to avoid burn out syndrome for programmers
- 10 useful tips for new programmers
- Syntax of JavaScript
- What is JavaScript? Can the Internet exist without JavaScript?
- 10 tips for using Codepen for beginners
- What is Currying in Javascript? How to use Currying in JavaScript
- Things to know about 'this' in JavaScript
- 6 useful tips from 11-year experienced programmers
- Udemy's top 5 JavaScript courses
- Top site with many good JavaScript exercises to practice
- What is JavaScript?
- Learn about ES6 in Javascript
- 7 great Windows 10 tips and tricks that you need to know
- How to Program