Knowledge in java coding

JavaScript Strings

JavaScript StringsA JavaScript string is zero or more characters written inside quotes.Examplevar x = "John Doe";You can use single or double quotes:Examplevar carName1 = "Volvo XC60";  // Double quotesvar carName2 = 'Volvo XC60';  // Single quotesYou can use quotes inside a string, as long as they don't match the quotes surrounding the string:Examplevar answer1 = "It's alright";var answer2 = "He is called 'Johnny'";var answer3 = 'He is called "Johnny"';String LengthTo find the length of a string, use the built-in length property:Examplevar txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";var sln = txt.length;

JavaScript String Methods

JavaScript String MethodsString methods help you to work with strings.String Methods and PropertiesPrimitive values, like "John Doe", cannot have properties or methods (because they are not objects).But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.String LengthThe length property returns the length of a string:Examplevar txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";var sln = txt.length;Finding a String in a StringThe indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string:Examplevar str = "Please locate where 'locate' occurs!";var pos = str.indexOf("locate");

IMP JS

Did You Notice?The two methods, indexOf() and search(), are equal?They accept the same arguments (parameters), and return the same value?The two methods are NOT equal. These are the differences:The search() method cannot take a second start position argument.The indexOf() method cannot take powerful search values (regular expressions).You will learn more about regular expressions in a later chapter.Extracting String PartsThere are 3 methods for extracting a part of a string:slice(start, end)substring(start, end)substr(start, length)The slice() Methodslice() extracts a part of a string and returns the extracted part in a new string.The method takes 2 parameters: the start position, and the end position (end not included).This example slices out a portion of a string from position 7 to position 12 (13-1):Examplevar str = "Apple, Banana, Kiwi";var res = str.slice(7, 13);The result of res will be:BananaRemember: JavaScript counts positions from zero. First position is 0.If a parameter is negative, the position is counted from the end of the string.This example slices out a portion of a string from position -12 to position -6:Examplevar str = "Apple, Banana, Kiwi";var res = str.slice(-12, -6);The result of res will be:BananaIf you omit the second parameter, the method will slice out the rest of the string:Examplevar res = str.slice(7);or, counting from the end:Examplevar res = str.slice(-12);

The substring() Method

The substring() Methodsubstring() is similar to slice().The difference is that substring() cannot accept negative indexes.Examplevar str = "Apple, Banana, Kiwi";var res = str.substring(7, 13);The result of res will be:BananaIf you omit the second parameter, substring() will slice out the rest of the string.The substr() Methodsubstr() is similar to slice().The difference is that the second parameter specifies the length of the extracted part.Examplevar str = "Apple, Banana, Kiwi";var res = str.substr(7, 6);The result of res will be:BananaIf you omit the second parameter, substr() will slice out the rest of the string.Examplevar str = "Apple, Banana, Kiwi";var res = str.substr(7);The result of res will be:Banana, KiwiIf the first parameter is negative, the position counts from the end of the string.Examplevar str = "Apple, Banana, Kiwi";var res = str.substr(-4);The result of res will be:KiwiReplacing String ContentThe replace() method replaces a specified value with another value in a string:Examplestr = "Please visit Microsoft!";var n = str.replace("Microsoft", "W3Schools");The replace() method does not change the string it is called on. It returns a new string.By default, the replace() method replaces only the first match:Examplestr = "Please visit Microsoft and Microsoft!";var n = str.replace("Microsoft", "W3Schools");By default, the replace() method is case sensitive. Writing MICROSOFT (with upper-case) will not work:Examplestr = "Please visit Microsoft!";var n = str.replace("MICROSOFT", "W3Schools");

JavaScript Numbers

JavaScript NumbersJavaScript has only one type of number. Numbers can be written with or without decimals.Examplevar x = 3.14;    // A number with decimalsvar y = 3;       // A number without decimalsExtra large or extra small numbers can be written with scientific (exponent) notation:Examplevar x = 123e5;    // 12300000var y = 123e-5;   // 0.00123JavaScript Numbers are Always 64-bit Floating PointUnlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:

JavaScript Number Methods

JavaScript Number MethodsNumber methods help you work with numbers.Number Methods and PropertiesPrimitive values (like 3.14 or 2014), cannot have properties and methods (because they are not objects).But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.The toString() MethodThe toString() method returns a number as a string.All number methods can be used on any type of numbers (literals, variables, or expressions):Examplevar x = 123;x.toString();            // returns 123 from variable x(123).toString();       // returns 123 from literal 123(100 + 23).toString();   // returns 123 from expression 100 + 23

The toExponential() Method

The toExponential() MethodtoExponential() returns a string, with a number rounded and written using exponential notation.A parameter defines the number of characters behind the decimal point:Examplevar x = 9.656;x.toExponential(2);     // returns 9.66e+0x.toExponential(4);     // returns 9.6560e+0x.toExponential(6);     // returns 9.656000e+0The parameter is optional. If you don't specify it, JavaScript will not round the number.The toFixed() MethodtoFixed() returns a string, with the number written with a specified number of decimals:Examplevar x = 9.656;x.toFixed(0);           // returns 10x.toFixed(2);           // returns 9.66x.toFixed(4);           // returns 9.6560x.toFixed(6);           // returns 9.656000toFixed(2) is perfect for working with money.The toPrecision() MethodtoPrecision() returns a string, with a number written with a specified length:Examplevar x = 9.656;x.toPrecision();        // returns 9.656x.toPrecision(2);       // returns 9.7x.toPrecision(4);       // returns 9.656x.toPrecision(6);       // returns 9.65600

The valueOf() Method

The valueOf() MethodvalueOf() returns a number as a number.Examplevar x = 123;x.valueOf();            // returns 123 from variable x(123).valueOf();       // returns 123 from literal 123(100 + 23).valueOf();   // returns 123 from expression 100 + 23In JavaScript, a number can be a primitive value (typeof = number) or an object (typeof = object).The valueOf() method is used internally in JavaScript to convert Number objects to primitive values.There is no reason to use it in your code.All JavaScript data types have a valueOf() and a toString() method.Converting Variables to NumbersThere are 3 JavaScript methods that can be used to convert variables to numbers:The Number() methodThe parseInt() methodThe parseFloat() methodThese methods are not number methods, but global JavaScript methods.Global JavaScript MethodsJavaScript global methods can be used on all JavaScript data types.These are the most relevant methods, when working with numbers:MethodDescriptionNumber()Returns a number, converted from its argument.parseFloat()Parses its argument and returns a floating point numberparseInt()Parses its argument and returns an integer

The Number() Method Used on Dates

The Number() Method Used on DatesNumber() can also convert a date to a number:ExampleNumber(new Date("2017-09-30"));  // returns 1506729600000The Number() method above returns the number of milliseconds since 1.1.1970.The parseInt() MethodparseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned:ExampleparseInt("10");         // returns 10parseInt("10.33");      // returns 10parseInt("10 20 30");   // returns 10parseInt("10 years");   // returns 10parseInt("years 10");   // returns NaN If the number cannot be converted, NaN (Not a Number) is returned.The parseFloat() MethodparseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned:ExampleparseFloat("10");        // returns 10parseFloat("10.33");     // returns 10.33parseFloat("10 20 30");  // returns 10parseFloat("10 years");  // returns 10parseFloat("years 10");  // returns NaNIf the number cannot be converted, NaN (Not a Number) is returned.Number PropertiesPropertyDescriptionMAX_VALUEReturns the largest number possible in JavaScriptMIN_VALUEReturns the smallest number possible in JavaScriptPOSITIVE_INFINITYRepresents infinity (returned on overflow)NEGATIVE_INFINITYRepresents negative infinity (returned on overflow)NaNRepresents a "Not-a-Number" value

JavaScript MIN_VALUE and MAX_VALUE

JavaScript MIN_VALUE and MAX_VALUEMAX_VALUE returns the largest possible number in JavaScript.Examplevar x = Number.MAX_VALUE;MIN_VALUE returns the lowest possible number in JavaScript.Examplevar x = Number.MIN_VALUE;JavaScript POSITIVE_INFINITYExamplevar x = Number.POSITIVE_INFINITY;POSITIVE_INFINITY is returned on overflow:Examplevar x = 1 / 0;JavaScript NEGATIVE_INFINITYExamplevar x = Number.NEGATIVE_INFINITY;

JavaScript Arrays

JavaScript ArraysJavaScript arrays are used to store multiple values in a single variable.Examplevar cars = ["Saab", "Volvo", "BMW"];What is an Array?An array is a special variable, which can hold more than one value at a time.If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:var car1 = "Saab";var car2 = "Volvo";var car3 = "BMW";However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?The solution is an array!An array can hold many values under a single name, and you can access the values by referring to an index number.Creating an ArrayUsing an array literal is the easiest way to create a JavaScript Array.Syntax:var array_name = [item1, item2, ...];      Examplevar cars = ["Saab", "Volvo", "BMW"];

JavaScript Date Objects

JavaScript Date ObjectsJavaScript Date OutputBy default, JavaScript will use the browser's time zone and display a date as a full text string:Sat Oct 12 2019 11:20:40 GMT+0530 (India Standard Time)You will learn much more about how to display dates, later in this tutorial.Creating Date ObjectsDate objects are created with the new Date() constructor.There are 4 ways to create a new date object:new Date()new Date(year, month, day, hours, minutes, seconds, milliseconds)new Date(milliseconds)new Date(date string)new Date()new Date() creates a new date object with the current date and time:Examplevar d = new Date();