Java Script

Interview Questions on Java Script

51. What is the use of substr()?

The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.
var str = "Hello world!";
var res = str.substring(1, 4);
res contains: ell

52. What is the use of toLowerCase()?

Converts all characters in string into lowercase.

53. What is the use of toUpperCase()?

Converts all characters in string into uppercase.

54. What is the use Of toString()?

Converts a value to string datatype.

55. How to remove extra white space?

Use trim().
Ex :- 
var str = "  Hello world!  ";
var res = str.trim();
res contains: Hello world!

56. How to convert string to float?

Float.parseFloat(“value”);
Ex:- 
var str =”3.5”;
var val = Float.parseFloat(str);

57. How to convert string to int?

Integer.parseInt(“value”);
Ex:- 
var str =”45”;
var val = Integer.parseInt(str);

58. How to create an array and access the value of an array?

Declare :var array1 = [2,5,7,3,5];
Access : var value1 = array1[0];

59. How to find the size of an array?

Syntax :- arrayname.legth;
Ex:- 
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var legth = fruits.length;
length contains 4.

60. How to sort the elements in alphabetical order in ascending?

Syntax : - arrayname.sort();
Ex:- 
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
// fruits contains : Apple,Banana,Mango,Orange

www.000webhost.com