JavaScript Tutorial List
JavaScript was once called the toy language, but after many updates, it is now being used to create much better quality applications. However, its name was previously published as LiveScript, Ekmascript, but now I know it as JavaScript.
Array:
An array is an object like a to-do list. The volume of JavaScript air depends on its components. Programmers can use it as they see fit. Cannot use string as index, but can use integers.
let items = [‘lap top’, ‘mobile phone’, ‘television’]
console.log(items.length)
//3
(1) array literal:
The syntax of creating an array using an array literal is given below:
As you can see, values are contained in [] and separated by, (comma).
Let’s see the simple example of creating and using arrays in JavaScript.
var emp=[“Sonoo”,”Vimal”,”Ratan”];
for (i=0;i<emp.length;i++){
document.write(emp[i] + “<br/>”);
}
(2) array directly:
Here, new keyword is used to create instances of the array.
Let’s see the example of creating arrays directly.
var i;
var emp = new Array();
emp[0] = “Arun”;
emp[1] = “Varun”;
emp[2] = “John”;
for (i=0;i<emp.length;i++){
document.write(emp[i] + “<br>”);
}
(3) array constructor:
Here, you need to create instances of the array by passing arguments in the constructor so that we don’t have to provide value explicitly.
var emp=new Array(“Jai”,”Vijay”,”Smith”);
for (i=0;i<emp.length;i++){
document.write(emp[i] + “<br>”);
}
String:
The JavaScript string is an object that represents a sequence of characters.
There are 2 ways to create a string in JavaScript
(1) By string literal:
The string literal is created using double quotes. The syntax of creating a string using string literal is given below
var str=”This is string literal”;
document.write(str);
(2) By string object:
The syntax of creating string objects using new keyword is given below:
var stringname=new String(“hello javascript string”);
document.write(stringname);
(1): Replace
:
The replace () method replaces a new string return pattern with some or all of the matches.
The replacement method does not change the string object. This provides a new string.
p.replace(‘old name’, ‘new name’)
(2): String indexof:
The JavaScript String indexOf() method returns the index position of the given string.
var s1=”javascript from javatpoint indexof”;
var n=s1.indexOf(“from”);
document.write(n);
(3): String toLowerCase:
The JavaScript String toLowerCase() method returns the given string in lowercase letters.
var s1=”JavaScript toLowerCase”;
var s2=s1.toLowerCase();
document.write(s2);
Number Object:
The JavaScript number object enables you to represent a numeric value. It may be integer or floating-point. The JavaScript number object follows the IEEE standard to represent the floating-point numbers.
var x = 122;
var x = 333;
Math:
JavaScript Math
The JavaScript math object provides several constants and methods to perform mathematical operations. Unlike date object, it doesn’t have constructors.
math.random
The JavaScript math.random() method returns the random number between 0 to 1.
math.floor:
The JavaScript math.floor(n) method returns the lowest integer for the given number. For example 3 for 3.7, 5 for 5.9 etc.
math.round
The JavaScript math.round(n) method returns the rounded integer nearest for the given number. If the fractional part is equal or greater than 0.5, it goes to upper value 1 otherwise lower value 0. For example 4 for 3.7, 3 for 3.3, 6 for 5.9 etc.