Strings :
A strings is a sequence of valid characters within a given character set. It is normally used to represent text. A string literal is defined by enclosing it in matching single or double quotes.
If a string contains no characters, it is called an empty string. It is normally created by two quote marks with nothing between them.
Some characters required in a string may not exist on the board or may be control characters. Control characters are represented by using a character or numeric value representing a character that is preceded by a backlash ( \ ) to indicate that it is a special character. Some escaped characters are as follows :
Escape Sequence Character
\0 = (blackslash zero) The Null character.
\b = Backspace
\t = Tab. Tabs behave erratically on the web and are best avoided, but are required at times.
\n = New line, Inserts a line break at the point specified.
\" = Double quote.
\' = Single quote, or an apostrophe, such as in can\'t.
\\ = The blackslash, since by itself it is a command.
Boolean :
There are two Boolean values, true and false. These are normally the result of a logical comparison in the code that returns a true/false or yes/no result.
Lifetime of Variables :
When variables in JavaScript are declared within a function, they can be accessed only within that function. When the function ends, the variable is destroyed. Such variables are called as local variables.
Local variables with the same name exist in different functions, because each is recognized only by the function in which it is declared.
If a variable is declared outside a function, all the functions on the page can access it. The lifetime of these variables starts when they are declared, and ends when the page us closed.
Let's see the example
Programe :
Output :
When attempting to add number data types to number data types to number stored as string, beware that your script will produce surprising unless you first do a little data type conversion. An example of this all too common mistake made by newcorners is when attempting to add number which came from fields.
In JavaScript the arithmetic operator '+' has the same symbol as the string operator, and one of the operands is a string value, the interpreter must make a standard method of evaluation of the expression. Therefore anytime a string operator are involved with a number, JavaScript interprets the '+' operator as a string operator and concatenates (joins) the number with the string. This means that the number and the string masquerading as a number will not be evaluated as a mathematical expression.
2 + 2 // result is 4
2 + "2" // result is 22
"2" + "2" // result is 22
2 + 2 + "2" // result is 42
No comments:
Post a Comment
If you have any doubts regarding this please comment or email us.