Categories
JavaScript Tips

Useful JavaScript Tips — Numbers, Strings, and Strict Mode

Spread the love

As with any kind of app, JavaScript apps have to be written well otherwise we will run into all kinds of issues later on. In this article, we’ll look at some tips we should follow to write JavaScript code faster and better.

Tip #1: Trim the Leading Zero in a Number String

We can trim the leading zero in a number by parsing the number string to a number.

To parse it into an integer, we can use the parseInt function.

For instance, we can write:

parseInt('010', 10)

Then we’ll parse the number string into a decimal.

If we want to parse a string into a number.

Then we can use the + operator. For instance, we can write:

+'010'

Then we get 10.

Also, we can string the leading 0 with regex and the replace method.

For instance, we can write:

number.replace(/^0+/, '')

^ means the start of the string.

Tip # 2: Replace All Occurrences of a String

We can replace all occurrences of a string with the replace method, a regex and the g flag for global replace.

For instance, we can write:

const phrase = 'I love dogs, dogs are great';  
const stripped = phrase.replace(/dog/g, 'cat');

Then we get “I love cats, cats are great” .

/dog/g is the regex. And 'cat' is what we replaced 'dog' with.

To perform a case insensitive search, we can add the i flag.

For instance, we can write:

const phrase = 'I love dogs. Dogs are great';  
const stripped = phrase.replace(/dog/gi, 'cat');

Then we get “I love cats. cats are great” .

We can also replace all substrings by using the split and join methods.

For instance, we can write:

const phrase = 'I love dogs, dogs are great';  
const stripped = `phrase.split('dog').join('cat')`;

We split the phrase string with 'dog' as the separator.

Then we call join with 'cat' so 'cat' replaces dog.

Therefore, stripped is “I love cats, cats are great” .

Tip # 3: Check if a Property is Undefined

If want to check if a property is undefined, then we can use the typeof operator.

For instance, we can write:

typeof color

Then if color is undefined, then it should return 'undefined' .

If we want to check an object property for undefined , then we can write:

typeof dog.color === 'undefined'

given that dog is an object.

Tip #4: Remove a Property from a JavaScript Object

We can remove a property from a JavaScript object by using the the delete operator.

To use it, we put delete in front of the property that we want to delete.

For instance, if we have the following object:

const dog = {  
  color: 'brown'  
}

Then we can write:

delete dog.color

Then we removed the color property from dog .

Tip #5: Redirect to Another Page

We can redirect to another page by setting a URL to the window.location object.

For instance, we can write:

window.location = 'https://example.com'

Then we go to https://example.com.

If we want to go to a relative path, then we can set the pathname property.

For instance, we can write:

window.location.pathname = '/foo'

Then we go to /foo .

We can also set the href property to go to a URL:

`window.location.href = 'https://`example`.com'`

Also, we can call the window.location.assign method to do the same thing:

window.location.assign('https://example.com')

Likewise, we can use the replace method. This rewrites the current page in history with the new URL.

We can write:

window.location.replace('https://example.com')

The browser also hs the self and top objects, which are the same as the window object.

So we can replace window with those objects.

Tip #6: Use Strict Mode

Strict mode disallows the use of some bad features in JavaScript.

To use it, we use the 'use strict' directive to enable it.

With strict mode, we can’t accidentally declare global variables.

For instance, we can’t write:

foo = 'bar';

with JavaScript strict mode on. It also disallows assignment to reserved keywords.

For example, we can’t write:

undefined = 1

Also, we can’t override object properties that are marked to be not writable.

For instance, if we have:

const dog = {}  
Object.defineProperty(dog, 'color', { value: 'brown', writable: false })

Then we can’t override the value by reassigning it.

Getter properties also can’t ve overridden with new values.

With strict mode on, we can’t add properties that we disallowed from adding properties with Object.preventExtensions .

Also, we can’t delete built-in objects with it.

Function arguments with the same name also can’t be in the same signature.

So we can’t write:

function(a, a, b) {  
  console.log(a, b)  
}

Conclusion

We can remove leading zero a number string by parsing it. There are various ways to replace all instances of a string. Strict mode is great for preventing us from adding bad code.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *