Category: JavaScript

String to Float Conversion in JavaScript

Floats are decimal numbers.  Similar to previous tutorial, if a float number is inside a quotation marks, it is treated as string by JavaScript.  To convert a string to float, we make use of parseFloat() function.  Example is given below. <html> <head> <title>JavaScript Tutorial </title> </head> <body> <script type="text/javascript"> var f1 = "1.35"; var f2

String to Integer Conversion in JavaScript

We have seen in previous tutorial that JavaScript treat a number as a string if you assign it using quotation marks.  Instead of addition of 2 numbers, it will perform concatenation.  To overcome this issue, we make use of parseInt() function in JavaScript.  This function will convert string to integer.  Example is given below. Note: 

String Concatenation in JavaScript

For string concatenation in JavaScript, we make use of plus (+) operator or concat function.  But the interesting part is, the plus (+) operator is also used for adding 2 numbers.  It all depends upon how you assign a value to a variable.  A string is always surrounded by quotes whereas a number does not

Comments in JavaScript

Comments in JavaScript are pretty much similar to PHP.  There are 2 types of comments, single-line comment and multi-line comment.  Single-line comment is useful in case of providing small information about the code.  Multi-line comment is useful in case of providing detailed information about the code.  Single-line comment starts with 2 forward slashes (//).  Multi-line

Placement of Script Tag in HTML File

It is a very common question where you should place your script tag.  Few people prefer placing it after head tag and few prefer placing it somewhere in the body tag.  If there is no manipulation of HTML elements on page load, then it is okay to place it in the head tag.  But if