Google

Data Types

Computers process information, by process, we mean the information is modified , filtered in some way . In computing the information is reffered to as data.

JavaScript supports a number of fundamental data types , These types can be broken down into two categories, primitive data types and composite data types.
 

Primitive Data Types

 Primitive data types are the simplest building blocks of a program.
numeric (1.50) , strings ("hello,world') , boolean (true, false)

there are two other special types that consist of a single value:
 Null , Undefined

Numeric literals

JavaScript supports both integers and floating-point numbers.

Integers are whole numbers ; e.g., 123 and –1. Integers can be expressed in decimal (base 10), octal (base 8), and hexadecimal (base 16), and are either positive or negative values.

Floating-point numbers are fractional numbers such as 123.45 or –1.5 .

String literals

String literals are characters enclosed in either double or single quotes. The quotes must be matched. If the string starts with a single quote, it must end with a matching single quote, and likewise if it starts with a double quote, it must end with a double quote. Single quotes can hide double quotes, and double quotes can hide single quotes:

"a string"
' another string' "
This is 'a string' "
'This is "a string" '
"5"

Strings can contain escape sequences (a single character preceded with a backslash),
 as follows:


 \'      single quotation mark
\"      double quotation mark
\n      new line
 \\      BackSlash


 Strings Concatenation

 this is the process of joining strings together, using a plus sign + . Its operands are two strings. If one string is a number and the other is a string, JavaScript will still concatenate them as strings. If both operands are numbers, the + will be the addition operator.

alert("java" + "script");
alert(5+5);
alert("5" + 5);


Booleans

Boolean literals are logical values that have only one of two values, true or false. You can think of the values as yes or no, on or off, or 1 or 0.

 if (a == false) { code; }


 Null

the null keyword represents no value  or "nothing," not even an empty string or zero .

Undefined

A variable that has been declared, but given no initial value, contains the value undefined and will produce a runtime error if you try to use it.

If you declare the variable and assign null to it, null will act as a placeholder and you will not get an error.

The typeof Operator

The typeof operator returns a string to identify the type of its operand (i.e., a variable, string, or object). You can use the typeof operator to check whether a variable has been defined because if there is no value assigned to the variable, the typeof operator returns undefined.

typeof (operand)

 Example:  
 typeof(54.6)
 typeof(str)
 typeof("str")

Composite Data Types

also called complex types, consist of more than one component. Objects, arrays, and functions,  all contain a collection of components. Objects contain properties and methods; arrays contain a sequential list of elements; and functions contain a collection of statements.

No comments: