DEV Community

Syed Mohsin Raza
Syed Mohsin Raza

Posted on • Edited on

JavaScript tips and tricks.

In this article we will look at some useful JavaScript tips and tricks.

location.reload()

This reloads the current document and works same as the reload button in your browser.This can be really handy when implementing a refresh button in the user interfaces.

const btnRefresh = document.querySelector('button')

btnRefresh.addEventListener('click',() => {
  location.reload()
})
Enter fullscreen mode Exit fullscreen mode

JavaScript styles

CSS styles applied using javascript are applied as inline styles.

<h1>Cakes & Bakes 🧁</h1> 
Enter fullscreen mode Exit fullscreen mode
document.querySelector('h1').style.color = "coral"

<h1 style="color: coral;">  // HTML Element
Enter fullscreen mode Exit fullscreen mode

Type coercion

Implicit conversion of values from one data type to another data type is named as type coercion i.e strings to numbers.

In case of plus operator values are concatenated and converted into strings.

console.log("2" * 10)  // output: 20
console.log(10 - '2')  // output: 8
console.log(2 + '2')   // output: '22'
Enter fullscreen mode Exit fullscreen mode

Active element

if you are having hard time figuring out which element is currently being focused use document.activeElement it returns the current focused element.

console.log(document.activeElement)
Enter fullscreen mode Exit fullscreen mode

Primitives

In JavaScript there are seven primitive data types.

number, string, boolean, undefined, null, BigInt, Symbol
Enter fullscreen mode Exit fullscreen mode

Remainder operator

Remainder operator % simply returns the remainder of a division i.e 5 % 2 = 1.You can use remainder operator to check either a number is even or odd

const number = 10

console.log(number % 2 === 0 ? 'Even ❀️' : 'Odd 🧑') 

// output: Even ❀️
Enter fullscreen mode Exit fullscreen mode

Design mode

Set document.designMode to on to make your webpage content editable.

document.designMode = "on" 
Enter fullscreen mode Exit fullscreen mode

Contains method

To check either a HTML element contains a specific class or not.

<h1 class="title">Page title</h1>
Enter fullscreen mode Exit fullscreen mode
document.querySelector('h1').classList.contains('title')
document.querySelector('h1').classList.contains('subtitle')

// output: true
// output: false 
Enter fullscreen mode Exit fullscreen mode

Var hoisting

Variables declared with var are hoisted but returns undefined.

console.log(a)
var a = 10;

// output: undefined
Enter fullscreen mode Exit fullscreen mode

Remove method

Remove methods allows you to remove an HTML from the document.

<h1>Page title βš™οΈ</h1> 
Enter fullscreen mode Exit fullscreen mode
const pageTitle = document.querySelector('h1')
pageTitle.remove()
Enter fullscreen mode Exit fullscreen mode

Eval method

Eval is a builtin Javascript function which allows you to evaluate the given values i.e strings, numbers.This can be used to build a simple calculator like this.

eval(2 * '5')  
// output: 10

eval(12 / '2')  
// output: 6 
Enter fullscreen mode Exit fullscreen mode

Typeof operator

The typeof operator allows you to check type of a value.

console.log(typeof 42);
// output: "number"

console.log(typeof 'markdown ⚑');
// output: "string"

console.log(typeof true);
// output: "boolean"

Enter fullscreen mode Exit fullscreen mode

Replace method

The replace method allows you to replace the very first instance of a string entity with the specified entity likewise replace we also have replaceAll that replaces all the instances.

const string = 'cake'
string.replace('c','b')  

// output: 'bake'
Enter fullscreen mode Exit fullscreen mode

Default parameters

Set default parameters for functions using assignment operator in case no argument is passed the function will return the default values.

I wrote this article to cover this topic in detail.

 function printName(name = "Anonymous"){
  console.log(name)
 }

 printName()  // output: "Anonymous"
Enter fullscreen mode Exit fullscreen mode

Document url

The document.URL returns the document URL/location as a string.

console.log(document.URL)

// output: "https://developer.mozilla.org/en-US/" 
Enter fullscreen mode Exit fullscreen mode

Strings index

Likewise arrays string indexes also start with 0.

let string = 'cake'

string[0]  // output: 'c'

string[1]  // output: 'a'
Enter fullscreen mode Exit fullscreen mode

Includes method

To check either a string or array contains a specific value or not. The method returns a boolean.

const string = 'JavaScript'

string.includes('J')  // output: true

const hearts = ['🧑', 'πŸ’™', '🀍']

console.log(hearts.includes('🧑'))  // output: true

console.log(hearts.includes('❀️'))  // output: false

Enter fullscreen mode Exit fullscreen mode

Hope you enjoyed reading this article! if you have something to say or have any questions feel πŸ’― free to comment below.

Top comments (6)

Collapse
 
haider436 profile image
Syed Ali Haider

very useful tips and tricks

Collapse
 
olamphzy profile image
Olamphzy

It is a great tips on JavaScript. I've it bookmark for constant remembrance

Collapse
 
imadusankadev profile image
Isuru Madusanka

Really useful post

Collapse
 
naucode profile image
Al - Naucode

Good post, it was a nice read, followed and bookmarked!

Collapse
 
mcbondgh profile image
mcbondgh

One thing about a beautiful language like JS is that it is so fun to work with.....

Collapse
 
solotoo profile image
vestan pance

why, in 2022 is anyone still using var ... let or const surely?