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.

4.5 ★ | 2 Vote

May be interested

  • Udemy's top 5 JavaScript coursesUdemy's top 5 JavaScript courses
    a programming language that runs on any computer in the world. a language does not need any special software to run. a language ranked among the top in the world.
  • Top site with many good JavaScript exercises to practiceTop site with many good JavaScript exercises to practice
    many people, after taking javascript courses and being equipped with some knowledge of the language they pursue, are eager to improve and cultivate these learned skills. so this article will give you a list of the top 3 websites to actually make javascript.
  • What is JavaScript?What is JavaScript?
    javascript is a programming language of html and web. it is lightweight and most commonly used as part of web pages, but their implementation allows client-side scripts to interact with users and create dynamic websites. it is an interpreted programming language with object-oriented capabilities.
  • ! = and! == What is the difference in JavaScript?! = and! == What is the difference in JavaScript?
    javascript includes operators like in other languages. an operator performs some operations on one or more operands (data values) and produces a result. today's article will help readers learn about 2! = and! == operators in javascript.
  • Learn about ES6 in JavascriptLearn about ES6 in Javascript
    es6 refers to version 6 of the ecma script programming language. ecma script is the standard name for javascript and version 6 is the next version after version 5, released in 2011.
  • 7 great Windows 10 tips and tricks that you need to know7 great Windows 10 tips and tricks that you need to know
    using windows 10 for a long time, but do you know these 7 extremely interesting tips and tricks? surely the tips below will surprise you
  • How to ProgramHow to Program
    as technology becomes more accessible, the demand for programmers is also increasing. programming is a skill that is cultivated and perfected over time. however, anyone must go through a certain initial step. there are countless languages ​​that are suitable for beginners, regardless of their chosen field (eg. javascript, etc. javascript is relatively advanced, so start with html or css). please continue reading to get started with your programming learning journey.
  • GitHub launches 'Copilot', an AI that can code with youGitHub launches 'Copilot', an AI that can code with you
    github has just officially launched a new ai programming tool called copilot. this tool will accompany programmers and developers to help them create more quality code in a variety of programming languages ​​such as python, javascript, typescript, ruby and go.
  • Summary of JavaScript exercises with sample codeSummary of JavaScript exercises with sample code
    in order to make your javascript learning easier, tipsmake.com has compiled a number of javascript exercises with sample solutions for you to practice.
  • JavaScript location in HTML FileJavaScript location in HTML File
    there is flexibility in providing javascript code anywhere in an html document. however, the most preferred ways to include javascript in an html file.