Tag Archives: JavaScript tutorial for beginners

Detect if JavaScript is Disabled


Detect if JavaScript is Disabled : It is very simple to detect if javascript is disabled in any browser. You can use <noscript> to detect the javascript. This is executed when javascript is disabled in your browser.


How to Detect if JavaScript is Disabled

You can check if javascript is disabled using the following tag as below –

Detect if JavaScript is Disabled Example:


Try it »

You can use the <noscript> tag as above. It will display the message “

JavaScript Disabled.

” if javascript is not enabled in any device’s browser.

Generate random number in JavaScript


Generate random number in JavaScript : Sometimes we need to generate random numbers in javascript. There are many ways to generate random number in javascript. Here in this tutorial we are going to explain how you can create random number in javascript using math function. We are going to create a function to generate random number in javascript which you can use anywhere.


How to Generate random number in JavaScript

You can generate random number using javascript as below

Generate random number using JavaScript Example:


Try it »

Here we have create a function getRandomNumber() which accepts two parameters start, end ie. range of number you want between. The above function will generate a random number between start(including) and end(including). So you can add any range that you wish to generate number between.

If you run the above example it will produce the random number between the given range. If you pass 0, 1000, it will produce any random number between 0, 1000. The output of the above example will look something like this –

How to Generate random number in JavaScript

Check if variable is array in JavaScript


Check if variable is array in JavaScript – There are many ways to check a variable is array or not using javascript. Here in this tutorial we are going to explain this using few methods with example and demo.


How to Check if variable is array in JavaScript

You can use the below method to check the variable whether it is array or not. It is simple and fast.

Check if variable is array in JavaScript Example:


Try it »

JavaScript Convert String to Integer


JavaScript Convert String to Integer – It is very simple to convert string to integer. There are various methods to convert a string into integer. Here in this tutorial we are going to explain how you can convert
string to integer using few most common methods. We will also present demo for each example.


JavaScript Convert String to Integer

You can convert string into integer using any one of the method –

Method 1 : Using parseInt()

This is most common method used to convert the strings into integer. This is the simple and best method to convert strings into the integer.

JavaScript Convert String to Integer Example:

var a = "10a"; // string
var resultInt = parseInt(a); // output as 10

Try it »

If you run the above example it will produce the output something like this –

JavaScript Convert String to Integer

Method 2 : Using Math.floor() function

You can also use Math.floor() function to convert strings into the integer.

JavaScript Convert String to Integer Example:

var a = "10.09"; // string
var resultInt = Math.floor(a); // output as 10

Try it »

If you run the above example it will produce the output something like this –

JavaScript Convert String to Integer Example

Check Undefined in JavaScript


Check Undefined in JavaScript : There are many ways to check undefined variable in javascript. Here in this tutorial we are going to explain the simplest methods to check for the undefined variables. In this tutorial we will explain this with example and demo.


How to Check Undefined in JavaScript | JS

You can use typeof to check if the variable is undefined.

Check Undefined in JavaScript : Example





Check 


Try it »

The above example will check the given variable using the typeof method. If the variable is undefined it will alert the above message.

If you run the above example it will produce the following output as below –

How to Check Undefined in JavaScript | JS

Trim String in JavaScript


Trim String in JavaScript – Few years ago there was no predefined function to trim in javascript so people adopted many ways to trim the strings. Some people liked to create own function and some used jQuery. But now there is no need to adopt alternative method to trim the strings in javascript string.trim() method is available to trim the strings. Here in this tutorial we are going to explain this with example and demo.


How to trim String in JavaScript Example

Trim is basically used to remove the white space from the string’s both end ie. starting and end point. You can use string.trim() function to trim the string as below-

Trim String in JavaScript Example:


Try it »

If you run the above example it will produce output something like this –

Trim String in JavaScript Example

The above function works perfectly for all strings and removes white space from both end.

JavaScript Calculate Distance Between Latitude Longitude Points


JavaScript Calculate Distance Between Latitude Longitude Points : Distance between two lat lon points can be calculated using the Haversine formula. Here in this tutorial we are going to explain how you can create a function to get the distance between two lat lon points. We are going explain this with example & online demo.


JavaScript Calculate Distance Between Latitude Longitude Points Formula

You can calculate distance between the latitude and longitude using the following formula-

Method 1 – Using Haversine Formula

Haversine formula is used to calculate distance between two points using the latitue and longitude-

JavaScript Calculate Distance Between Latitude Longitude Points Example:


Try it »

The above example will give you the distance between two latitude longitude points.

Method 2

You can also use the below method to get the distance between the lat, lon points –

JavaScript Calculate Distance Between Latitude Longitude Points Example:


Try it »

The above example will give you the distance between two latitude longitude points. If you run the above example it will output something like this –

JavaScript Calculate Distance Between Latitude Longitude Points

JavaScript Format Number To Currency


JavaScript Format Number To Currency : There are many ways to format number to currency. Here in this tutorial we are going to explain how you can format number to currency using the javascript functions and other possible solutions. We will create demo for each example you can try each example to see the output.


JavaScript Format Number To Currency Example

You format number to currency using the below methods.

You can use javascript function .toFixed(decimalPoints) to round up the number upto the given decimal points. Here is a simple example –

JavaScript Format Number To Currency Example:





Click to Format

Output for 98.402 =

Try it »

The above function accepts the two parameters – first number to be converted and second currency symbol. If you run the above example it will produce output something like this –

JavaScript Format Number To Currency Money

JavaScript Open Url in New Tab


JavaScript Open Url in New Tab : We sometimes need to open url in new tab using the javascript. There are many ways to do this. Here in this tutorial we are going to follow the simple example. We are going to explain how you can open url in new tab using javascript. We will explain this functionality with example and online demo with our online editor.


JavaScript Open Url in New Tab Example

Let us create a simple function which will redirect to the given url.

JavaScript Open Url in New Tab:


Try it »

If you run the above example it will produce output something like this and when you click on link it will open the url in the new tab.

JavaScript Open Url in New Tab Example