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
- 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
May be interested
- What is the difference between Java and JavaScript?although the names of java and javascript seem to be related (javascript seems to be a script in java?), but that's the only similarity. these two languages are not technically related to each other.
- How to Code a Phrasal Template Word Game Using Javascriptphrasal word template games are game in which the player fills in a random part of speech, not knowing what it will be applied to. the most popular example of this is madlibs. this is a fun game to code and play, and you will be proud once...
- How to Debug Javascriptthe ability to debug code is an essential tool in any programmer's arsenal. debugging will give you inside information into how your program is functioning, in real-time, as it runs. javascript offers a unique but familiar method of...
- How to Use jQuery on Your Websitejquery is the most popular javascript language by far, used by many popular websites across the internet. however, since it is a library, it is not a part of unaltered javascript. how, then, can you use jquery on your website? the process...
- How to Enable JavaScript on a Macthis wikihow teaches you how to enable javascript in safari, google chrome, and mozilla firefox when you're using a mac. javascript is enabled by default in most modern web browsers, so you shouldn't have to enable it unless you've turned...
- How to Enable JavaScript in Mozilla Firefoxjavascript is a core feature of many websites, especially modern interactive websites. disabling it can be done for speed and bandwidth concerns, security, or even to ensure that the site works with a screen reader for visually impaired...