JavaScript for Beginners - Basic Concepts (Data Types) (P: VI)
Data Types
The term data type
refers to the types of values with which a program can work. JavaScript
variables can hold many data types, such as numbers, strings, arrays and many more.
Unlike many other programming languages, JavaScript dose not
define different types of numbers like integers, short, long, floating-point,
etc.
Numbers
JavaScript numbers can be written with or without decimals.
JavaScript numbers can also have decimals
Result:
Note: JavaScript numbers are always stored as double
precision floating point numbers.
Strings
JavaScript strings are used for storing and manipulating
text.
A string can be any text that appears within quotes. You can single or double quotes.
You can use quotes inside a string as they don’t match the quotes surrounding the string.
As strings, must be written within quotes, quotes inside the
string must be handled. The backslash (\) escape character turns special
characters into string characters.
Result:
Note: If you begin a string with a single quote, then you
should also end it with a single quote. The same rule applies to double quotes.
Otherwise, JavaScript will become confused.
The escape character (\) can also be used to insert other
special characters into a string. These special characters can be added to a
text string using the backslash sign
Code |
Outputs |
\’ |
Single Quote |
\” |
Double
quote |
\\ |
Backslash |
\n |
New line |
\r |
Carriage return |
\t |
Tab |
\b |
Backspace |
\f |
Form end |
Boolean
In JavaScript boolean, you can have one of two values, either
true of false. These are useful when you need a data type that can only have
one of two values such as Yes/No, On/Off, True/False.
Example
Note: The boolean value of 0 (zero), null, undefined, empty string is false. Everything with a real value is true
Comments
Post a Comment