7 useful links for javascript
Tags: javascript
Published: 2019-06-20 09:50:19

---
title: 7 useful links for javascript
published: true
description: A selection of useful links for the development of JavaScript
tags: javascript
---

**1. Reduction of values to a logical type**
Here's how to bring a certain value to a logical type:

```javascript
const myBoolean = !!myVariable;
```

Double negation (!!) is necessary in order for a value that is true from the point of view of JavaScript rules to be converted to true and false to false.

**2. Remove duplicate values ​​in arrays**
Here's how to remove duplicate values ​​from an array:

```javascript
const deDupe = [... new Set (myArray)];
```

The specified data structures store only unique values. As a result, the use of such a data structure and syntactic scattering allows you to create a new array based on the array myArray, in which there are no duplicate values.

**3. Creating and setting object properties by condition**

To set properties of objects using the && operator, you can use the distribution syntax:

```javascript
const myObject = {... myProperty && {propName: myProperty}};
```

If as a result of the calculation of the left side of the expression, something is received that is perceived by JS as a false value, then && will not perform further calculations, and the new property will not be created and set. MyObject will be empty. If the ... myProperty construction returns some result that JS perceived as true, thanks to the && construction, the propName property will appear in the object, preserving the resulting value.

**4. Merge objects**

Here's how to create a new object in which two other objects will be merged:

```javascript
const mergedObject = {... objectOne, ... objectTwo};
```

This approach can be used to organize the merger of an unlimited number of objects. Moreover, if objects have properties with the same name, in the final object there will be only one such property belonging to the source objects, which is located to the right of the others. Please note that this is done using shallow copying of object properties.

**5. Exchange of variable values**

To exchange values ​​between two variables without using an auxiliary variable, you can do this:

```javascript
[varA, varB] = [varB, varA];
```

After that, what was in varA will fall into varB, and vice versa. This is possible through the use of internal mechanisms of destruction.

**6. Removing False Values ​​from an Array**

Here's how to remove from the array all values ​​that are considered false in JavaScript:

```javascript
const clean = dirty.filter (Boolean);
```

During the execution of this operation, values ​​such as null, undefined, false, 0, as well as empty lines will be removed from the array.

**7. Converting numbers to strings**

To convert numbers stored in an array to their string representation, you can do this:

```javascript
const stringArray = numberArray.map (String);
```

The string elements of the array during such a conversion will remain string.

You can also perform the inverse transform by converting String values ​​to Number values:

const numberArray = stringArray.map (Number);

**Conclusion: on merging and expanding a single-line code**

What examples of useful JS one-line users would you add to this material?