JavaScript

1. Java Script Module 2. World Wide Web (WWW) 3. Web Terms 4. NEED FOR SCRIPT 5. Introduction to Internet Programming 6. CLIENT SIDE and SERVER SIDE Scripting 7. Different Scripting Languages 8. Introduction to JavaScript 9. JavaScript and Security 10. How to Enable JavaScript 11. Advantages of Java Script 12. Limitations with JavaScript 13. Difference between JavaScript and JAVA 14. Difference between JavaScript and VBScript 15. JavaScript Development Tools 16. JavaScript Programming Techniques 17. JavaScript terms 18. Starting with JavaScript Programming step by step 19. Time to start JavaScript Programming 20. JavaScript Variables 21. JavaScript RESERVED WORDS 22. JavaScript Data Type 23. NUMBER DATA TYPE 24. BOOLEAN DATA TYPE 25. STRING DATA TYPE 26. NULL DATA TYPE 27. JavaScript Undefined DATA TYPE 28. JavaScript Operators 29. JavaScript Arithmetic Operators 30. JavaScript Logical Operators 31. JavaScript Comparison Operators 32. JavaScript String Operators 33. Bit Manipulation Operators 34. JavaScript Assignment Operators 35. JavaScript Conditional Operator 36. Conversion between JavaScript Data Types 37. Alert JavaScript function 38. confirm JavaScript function 39. prompt JavaScript function 40. JavaScript Statements 41. Single line & Multi-line Comments 42. JavaScrip If Statement 43. JavaScript if..else Statements 44. JavaScript If…else..if statement. 45. JavaScript For Statement 46. JavaScript For..in Statement: 47. JavaScript While Statement 48. JavaScript do while Statement: 49. JavaScript break and continue statement 50. JavaScript switch Statement: 51. JavaScript function() 52. JavaScript function with a Return Value 53. JavaScript with Statement 54. try, catch, and throw Statements 55. JavaScript ARRAY 56. JavaScript array of different data types 57. JavaScript ARRAY and Bubble sorting Program 58. JavaScript Array Methods 59. concate() method 60. every() method 61. filter() method 62. forEach() method 63. indexOf() method 64. Join() method 65. lastIndexOf() method 66. map() method 67. reduce() method 68. reduceRight() method 69. reverse() method 70. some() method 71. toSource() method 72. toString() method 73. pop() method 74. push() method 75. shift () method 76. slice () method 77. splice () method 78. sort() method 79. unshift() method 80. EVENT handling with JavaScript 81. onblur JavaScript Event 82. onchange JavaScript Event 83. onclick JavaScript Event 84. onDblClick JavaScript Event 85. onfocus JavaScript Event 86. onkeydown JavaScript Event 87. onkeyup JavaScript Event 88. Onkeypress JavaScript Event 89. onLoad JavaScript Event 90. onMousedown JavaScript Event 91. onMouseup JavaScript Event 92. Onmouseover JavaScript Event 93. onmouseout JavaScript Event 94. onMouseMove JavaScript Event 95. onReset JavaScript Event 96. onsubmit JavaScript Event 97. onUnload JavaScript Event 98. The Screen Object 99. JavaScript Interview Part1 100. JavaScript Interview Part2 101. JavaScript Interview Part3 102. JavaScript Interview Part4 103. JavaScript Interview Part5 104. JavaScript Interview Part6 105. JavaScript Interview Part7 106. JavaScript Interview Part8 107. JavaScript Interview Part9 108. JavaScript Interview Part10
Pr.Pg Next Pg

Conversion between JavaScript Data Types tutorials

  • JavaScript converts values from one type of data to another when they are used in expression. There are two type of Conversion that can take place in JavaScript.

    1. Automatic Conversion of types

    2. Explicit Conversion of types

 

1) Automatic Conversion of Data types

  • JavaScript automatically convert data from one type to another type as and when required.

  • It tries to perform the type conversion in a way that makes some sense of expression.

  • For example when you tries to add number to string, it converts number to string and then joins it.

For example: "sam"+ 10 , will convert 10 to "10", and then it will be joined to "sam" making it "sam10".

 

Example: JavaScript Example shows the automatic conversion of type.
Output:

Numeric a equal to 10

String b equal to 25

 

c=a+a equal to 20

d=b+b equal to 2525

e=a+b equal to 1025

f=b+a equal to 2510

 

  • From the above example we have declare variables a, b,c,d,e and f . Where a will have numeric 10 value, and b will have character "25". In the first document.write it will write the output value of a =10 .

  • In the second document.write the value of b =25 where b is the string.

  • In the third document.write the value of c =20 because it adds two numeric values. In the fourth document.write the value of d =2525 because it adds two string values. Similarly e= 1025 addition of numeric and string. And f=2510 addition of string and numeric values.

  • Please refer following rules when String operands are used with non-string operator.

Numeric values are converted to appropriate String Value

Boolean values are converted to 1 for true and 0 for false.

Null values are converted to "null" for string operation, False for Logical operation and 0 for Numeric operation.

 

2) Explicit Conversion Functions:

  • Functions are collections of JavaScript code that perform a particular task, and often return a value. A function may take zero or more parameters,

  • These parameters are used to specify the data to be processed by the function.

  • JavaScript provides three functions which are used to perform explicit type conversion.

parseInt()

parseFloat()

eval()

 

 

a) parseInt()

  • The parseInt () function is used to convert a string value into an integer.unlike eval() , parseInt() returns the first integer contained in a string or 0 if the string does not begin with an integer.

 

Syntax:

parseInt (string, radix)

  • There are two arguments, one of which is mandatory string argument that is the string, which is parsed to return the integer.

  • The other argument is the, optional radix argument that specifies the base of the integer.

  • By default it is decimal radix is 10.

  • For binary radix is 2.

  • For Octal radix is 8.

  • For Hexadecimal radix is 16.

  • Only the first number in the string is returned.

  • String leading and trailing spaces are trimmed.

  • If the first character cannot be converted to number, parseInt() return NaN.

  • The parseInt () function is supported in all major browsers.like Internet Explorer,Chrome,FireFox,Opera,Safari etc.

 

Example: JavaScript Example shows the use of parseInt () function.
Output:

21

25

987

865

14

NaN

11

15

NaN

 

b) parseFloat()

  • parseFloat() function is used to convert string type data to numeric Floating point data (with Decimal). So parseFloat("13.65") returns 13.65, parseFloat("32e3abc") returns 32000 while parseFloat("abc’) returns 0.

  • This function is used to convert string into numeric value with decimal if any. parseInt() will ignore decimal value, while parseFloat() will consider it.

 

Syntax:

parseFloat(string)

Only the first number in the string is returned.

String leading and trailing spaces are trimmed.

If the first character cannot be converted to number, parseFloat() return NaN.

The parseFloat() function is supported in all major browsers.like Internet Explorer,Chrome,FireFox,Opera,Safari etc.

 

Example: JavaScript Example shows the use of parseInt() and parseFloat () function.
Output:

A equal to 10

B equal to 4

parseInt(a/b) equal to 2

parseFloat(a/b) equal to 2.5

Example: JavaScript Example shows the use of parseFloat () function.
Output:

21

23

234.56

432.75

543

652.234

NaN

230000

 

c) eval()

  • eval() function stands for evaluate. It is used to convert string expression or formula to numeric value.

 

Syntax:

eval(JavaScript expression, variable, statements or formula)

  • expr: The expr is a string represents a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.

Example: JavaScript Example shows the use of eval() function.
Output:

5

25

15

Example: JavaScript Example shows the use of eval() function.
Output:

a equal to 20

Pr.Pg border                                              Next Pg