Tampilkan postingan dengan label Java Script Types. Tampilkan semua postingan
Tampilkan postingan dengan label Java Script Types. Tampilkan semua postingan

Minggu, 30 September 2012

JavaScript Boolean Object

Complete Boolean Object Reference

For a complete reference of all the properties and methods that can be used with the Boolean object.
The reference contains a brief description and examples of use for each property and method!

Create a Boolean Object

The Boolean object represents two values: "true" or "false".
The following code creates a Boolean object called myBoolean:


If the Boolean object has no initial value, or if the passed value is one of the following:
  • 0
  • -0
  • null
  • ""
  • false
  • undefined
  • NaN
the object is set to false. For any other value it is set to true (even with the string "false")!

JavaScript String Object

String Object


The String object is used to manipulate a stored piece of text.
Examples of use:
The following example uses the length property of the String object to find the length of a string:

var txt="Hello world!";
document.write(txt.length);
 
The code above will result in the following output:

12
 
The following example uses the toUpperCase() method of the String object to convert a string to uppercase letters:

var txt="Hello world!";
document.write(txt.toUpperCase());
 
The code above will result in the following output:

HELLO WORLD!


Special Characters

The backslash (\) can be used to insert apostrophes, new lines, quotes, and other special characters into a string.
Look at the following JavaScript code:

var txt="We are the so-called "Vikings" from the north.";
document.write(txt);
 
In JavaScript, a string is started and stopped with either single or double quotes. This means that the string above will be chopped to: We are the so-called
To solve this problem, you must place a backslash (\) before each double quote in "Viking". This turns each double quote into a string literal:

var txt="We are the so-called \"Vikings\" from the north.";
document.write(txt);
 
JavaScript will now output the proper text string: We are the so-called "Vikings" from the north.
The table below lists other special characters that can be added to a text string with the backslash sign:



JavaScript Numbers

JavaScript Numbers

JavaScript numbers can be written with, or without decimals:

Example

var pi=3.14;    // Written with decimals
var x=34;       // Written without decimals
Extra large or extra small numbers can be written with scientific (exponent) notation:

Example

var y=123e5;    // 12300000
var z=123e-5;   // 0.00123


All JavaScript Numbers are 64-bit

JavaScript is not a typed language. Unlike many other programming languages, it does not define different types of numbers, like integers, short, long, floating-point etc.
All numbers in JavaScript are stored as 64-bit (8-bytes) base 10, floating point numbers.

Precision

Integers (numbers without a period or exponent notation) are considered accurate up to 15 digits.
The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:

Example

var x=0.2+0.1;



Octal and Hexadecimal

JavaScript interprets numeric constants as octal if they are preceded by a zero, and as hexadecimal if they are preceded by a zero and and x.

Example

var y=0377;
var z=0xFF;

lamp Never write a number with a leading zero, unless you want an octal conversion.