Hiển thị các bài đăng có nhãn javascript Beginner. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn javascript Beginner. Hiển thị tất cả bài đăng

Extra small numbers in javascript

     Big numbers can write using the below format in javascript.




Example Program:- (Editor)


Editor is Loading...

Advertisement

How to convert Hexadecimal, octal,binary

     In javascript we can easy to convert any numbers to corespoinding hexadecimal, octal or binary value using toString() method.

Syntax

   variable.toString()      //convert to String
   variable.toString(2)    //convert to Binary
   variable.toString(8)    //convert to Octal
   variable.toString(16)  //convert to Hexadecimal

Example

   var num=66;
   num.toString();     //return "66"
   num.toString(2);   //return "1000010"
   num.toString(8);   //return "102"
   num.toString(16); //return "42"


Example Program:- (Editor)


Editor is Loading...

Advertisement

parseInt() in javascript

     The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

Syntax

   parseInt(string, radix);

string
     The value to parse. If string is not a string, then it is converted to a string (using the ToString abstract operation). Leading whitespace in the string is ignored.

radix
     An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.

Description
     The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN. If not NaN, the returned value will be the decimal integer representation of the first argument taken as a number in the specified radix (base). For example, a radix of 10 indicates to convert from a decimal number, 8 octal, 16 hexadecimal, and so on. For radices above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.

     If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.


  1. If radix is undefined or 0 (or absent), JavaScript assumes the following:
  2. If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
  3. If the input string begins with "0", radix is eight (octal) or 10 (decimal).  Exactly which radix is chosen is implementation-dependent.  ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet.  For this reason always specify a radix when using parseInt.
  4. If the input string begins with any other value, the radix is 10 (decimal).
  5. If the first character cannot be converted to a number, parseInt returns NaN.


For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.

To convert number to its string literal in a particular radix use intValue.toString(radix).

The following example return 15.
Example

   parseInt(" 0xF", 16);
   parseInt(" F", 16);
   parseInt("17", 8);
   parseInt(021, 8);
   parseInt("015", 10);
   parseInt(15.99, 10);
   parseInt("15,123", 10);
   parseInt("FXX123", 16);
   parseInt("1111", 2);
   parseInt("15*3", 10);
   parseInt("15e2", 10);
   parseInt("15px", 10);
   parseInt("12", 13);

The following example return NaN.
Example

   parseInt("Hello", 8); // Not a number at all
   parseInt("546", 2);   // Digits are not valid for binary representations

The following example return -15.
Example

   parseInt("-F", 16);
   parseInt("-0F", 16);
   parseInt("-0XF", 16);
   parseInt(-15.1, 10);
   parseInt(" -17", 8);
   parseInt(" -15", 10);
   parseInt("-1111", 2);
   parseInt("-15e1", 10);
   parseInt("-12", 13);

The following example return 224.
Example

   parseInt("0e0", 16);


Example Program:- (Editor)


Editor is Loading...

Advertisement

parseFloat() in javascript

     The parseFloat() function parses a string argument and returns a floating point number. parseFloat is a top-level function and is not associated with any object.

     parseFloat parses its argument, a string, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, it returns the value up to that point and ignores that character and all succeeding characters. Leading and trailing spaces are allowed.

     If the first character cannot be converted to a number, parseFloat returns NaN.

     For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseFloat is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.

     parseFloat can also parse and return the value Infinity. You can use the isFinite function to determine if the result is a finite number (not Infinity, -Infinity, or NaN).

Syntax

   parseFloat(string);
The following all examples are return 3.14
Example

   parseFloat("3.14");
   parseFloat("314e-2");
   parseFloat("0.0314E+2");
   parseFloat("3.14more non-digit characters");


Example Program:- (Editor)


Editor is Loading...

Advertisement

toFixed() in javascript

     The toFixed() method formats a number using fixed-point notation.

digits
Optional. The number of digits to appear after the decimal point; this may be a value between 0 and 20, inclusive, and implementations may optionally support a larger range of values. If this argument is omitted, it is treated as 0.

Returns
A string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length. If numObj is greater than 1e+21, this method simply calls Number.prototype.toString() and returns a string in exponential notation.

Throws

RangeError
If digits is too small or too large. Values between 0 and 20, inclusive, will not cause a RangeError. Implementations are allowed to support larger and smaller values as well.

TypeError
If this method is invoked on an object that is not a Number.

Syntax

   number.toFixed([digits]);
Example

   (6.3652128).toFixed(2);  //6.37


Example Program:- (Editor)


Editor is Loading...

Advertisement

toPrecision() in javascript

     The toPrecision() method returns a string representing the Number object to the specified precision.

     A string representing a Number object in fixed-point or exponential notation rounded to precision significant digits. See the discussion of rounding in the description of the Number.prototype.toFixed() method, which also applies to toPrecision().

     If the precision argument is omitted, behaves as Number.prototype.toString(). If the precision argument is a non-integer value, it is rounded to the nearest integer.

Syntax

   number.toPrecision([precision]);
Example

   (3.666621).toPrecision(2)  //3.37


Example Program:- (Editor)


Editor is Loading...

Advertisement

toExponential() in javascript

     The toExponential() method returns a string representing the Number object in exponential notation.

Parameters
fractionDigits
     Optional. An integer specifying the number of digits after the decimal point. Defaults to as many digits as necessary to specify the number.
Returns

     A string representing a Number object in exponential notation with one digit before the decimal point, rounded to fractionDigits digits after the decimal point. If the fractionDigits argument is omitted, the number of digits after the decimal point defaults to the number of digits necessary to represent the value uniquely.

     If you use the toExponential() method for a numeric literal and the numeric literal has no exponent and no decimal point, leave a space before the dot that precedes the method call to prevent the dot from being interpreted as a decimal point.

     If a number has more digits than requested by the fractionDigits parameter, the number is rounded to the nearest number represented by fractionDigits digits. See the discussion of rounding in the description of the toFixed() method, which also applies to toExponential().

Throws

RangeError
     If fractionDigits is too small or too large. Values between 0 and 20, inclusive, will not cause a RangeError. Implementations are allowed to support larger and smaller values as well.
TypeError
     If this method is invoked on an object that is not a Number.

Syntax

   numberobject.toExponential([fraction_digit]);
Example

var numObj = 77.1234;
console.log(numObj.toExponential());  // logs 7.71234e+1
console.log(numObj.toExponential(4)); // logs 7.7123e+1
console.log(numObj.toExponential(2)); // logs 7.71e+1
console.log(77.1234.toExponential()); // logs 7.71234e+1
console.log(77 .toExponential());     // logs 7.7e+1


Example Program:- (Editor)


Editor is Loading...

Advertisement

Math.min() in javascript

     The Math.min() function returns the smallest of zero or more numbers. min() is a static method of Math, you always use it as Math.min(), rather than as a method of a Math object you created (Math is not a constructor).

     If no arguments are given, the result is Infinity.
     If at least one of arguments cannot be converted to a number, the result is NaN.

Syntax

   Math.min([value1,value2,...]);
Example

   Math.min(20,50,36,2);  //2
   Math.min(-3,3,0,99);  //-3


Example Program:- (Editor)


Editor is Loading...

Advertisement

Math.max() in javascript

     The Math.max() function returns the largest of zero or more numbers. max() is a static method of Math, you always use it as Math.max(), rather than as a method of a Math object you created (Math is not a constructor).

     If no arguments are given, the result is -Infinity.
     If at least one of arguments cannot be converted to a number, the result is NaN.

Syntax

     Math.max([value1,value2,value3,...]);
Example

Math.max(50, 10);   //  50



Math.max(-1, -200); // -1



Math.max(-999, 2);  //  2


Example Program:- (Editor)


Editor is Loading...

Advertisement

Math.floor() in javascript

     The Math.floor() function returns the largest integer less than or equal to a given number. floor() is a static method of Math, you always use it as Math.floor(), rather than as a method of a Math object you created (Math is not a constructor).

Syntax

   Math.floor(value);
Example

   Math.floor( 40.95); //  40
   Math.floor(-48.95); // -49


Example Program:- (Editor)


Editor is Loading...

Advertisement

Math.random() in javascript

     The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1] that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

Note:  Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. 

Syntax

   Math.random();
Example

   Math.random();  //0 to 1 Numbers

   (Math.floor(Math.random() * 100) + 1) // 1 to 100 Numbers


Example Program:- (Editor)


Editor is Loading...

Advertisement

Math.round() in javascript

     The Math.round() function returns the value of a number rounded to the nearest integer. If the fractional portion of number is 0.5 or greater, the argument is rounded to the next higher integer. If the fractional portion of number is less than 0.5, the argument is rounded to the next lower integer.

     Because round() is a static method of Math, you always use it as Math.round(), rather than as a method of a Math object you created (Math has no constructor).

Syntax

   Math.round(number);
Example

Math.round(19.39); //19

Math.round(19.7); //20

Math.round(-19.04); //-19

Math.round(-19.51); //-20


Example Program:- (Editor)


Editor is Loading...

Advertisement

Math.ceil() in javascript

     The Math.ceil() function returns the smallest integer greater than or equal to a given number. ceil() is a static method of Math, you always use it as Math.ceil(), rather than as a method of a Math object you created (Math is not a constructor).

Syntax

   Math.ceil(number);


Example

   Math.ceil(.97);   // 1

   Math.ceil(0.3);     // 1

   Math.ceil(3);     // 3

   Math.ceil(7.003); // 8


Example Program:- (Editor)


Editor is Loading...

Advertisement

abs() pow() and sqrt() in javascript

Math.abs()    - The Math.abs() function returns the absolute value of a number.
Math.pow()  - The Math.pow() function returns the base to the exponent power, that is, baseexponent.
Math.sqrt()   - The Math.sqrt() function returns the square root of a number.




Example Program:- (Editor)


Editor is Loading...

Advertisement

Simple example for array in javascript

     The JavaScript Array object is a global object that is used in the construction of arrays; which are high-level, list-like objects.

Example

var ar=[1,2,3,4,5];


Example Program:- (Editor)


Editor is Loading...

Advertisement