A character set is basically a list of characters recognized by computer hardware and software. It is represented by a number. Earlier ASCII was used as standard character set encoding for web pages. There were lots of character encoding problems that were almost solved after the arrival of HTML5 and XML. JavaScript provides support for different types of languages and their characters. The character set attribute shows the character encoding in external files. For HTML5, there is a default character set encoding is UTF-8.
There are some common values of character set encoding such as the following:
For example, if your frontend page is in Spanish, and you do not use the character set property in your page, then it will not show some special characters in Spanish clearly. For this, you must declare a character set property in the top <head>
tag of your page, as follows:
<meta charset="utf-8">?
In JavaScript, the charset
property returns the character encoding of the current document. The syntax for reading the characterSet
property from the document is document.characterSet;
.
We can define the
characterSet
attribute in the <script>
tag in the parent page of our website, as follows:
<script type="text/javascript"charset="utf-8"></script>
HTML is not a case-sensitive language, but XML and JavaScript are case sensitive languages.
JavaScript is a case-sensitive language. Case sensitivity in JavaScript does not only apply to variable names but also to JavaScript keywords and JavaScript's event handlers. For example, if you have the firstname
and firstName
variables, then these will be two different variables. In JavaScript, while calling a function, you must write its name exactly the same way as it was defined, matching the letter case.
A popular convention also used in JavaScript's primitive methods is to use camel case, in which a phrase is written with the first letter of the first word in lowercase and every successive word has an uppercase first letter, which makes it easier to read.
Examples of some functions are as follows:
toUpper();
toArray();
JavaScript has functions that are built into the language; they take strings and transform them to the uppercase or lowercase. This can make handling strings easier to manipulate when the input has varying cases. The following are the two parameters the functions can take:
.toUpperCase()
.toLowerCase()
The .toUppercase()
function will convert letters into uppercase and the .toLowerCase()
function will convert letters into lowercase. These are the built-in and case-sensitive functions of JavaScript.
Whitespaces and line breaks are used to format and indent code in a neat and consistent manner so that the code becomes readable and easy to understand.
Spaces, Tabs, New lines which are not a part of a string are called whitespaces. JavaScript removes whitespaces and line breaks between tokens in the programs, Spaces and Line breaks that are a part of a string are not removed when script is executed.
In any text, there are three types of line breaks:
or as
or
.
These line breaks occur in different types of operating systems:
is usually created on Windows
is usually created on OS XIf you want to remove line breaks from any text, then we must deal with all types of line breaks because the text could be from these three sources Windows, Linux, and Mac.
We have several methods to remove line breaks from any text. For example, we have the following:
varabc = abc.replace(/( | | )/gm,"");
In this code, we have used a regular expression for removing line breaks from our text. This will remove line breaks
, then
, and finally
. We used the suffix gm
at the end of the regular expression because m
shows that line breaks should be removed from all the text and g
shows that it should be done more than once.
Regular expressions are discussed in detail in Chapter 8, JavaScript Expressions, Operators, Statements, and Arrays.
Now, there are some spaces between texts; you can remove these whitespaces using JavaScript. There are different functions of JavaScript for removing all leading spaces from a string; you can use the following:
str = str.replace(/^s+|s+$/g,'');
This piece of code uses a regular expression, which checks multiple occurrences of white spaces at beginning and end of string.
The g
in this regular expression shows that the global search should be performed while checking the text. There are more methods, such as the following:
string.replace()
function is used to replace all leading whitespaces with an empty stringstr.trim()
function is used to remove spaces from both sides of a stringEvery Unicode escape sequence consists of six characters with a much defined syntax: four characters following u
. Smaller code is accompanied by leading zeroes but the length of the sequence is maintained. For example, u00a9
has two leading zeros. Similarly, the copyright symbol can be represented as u00A9
.
Any character with a code point that is less than 65536 can be escaped using the hexadecimal value of its code point, prefixed with u
.
The JavaScript Unicode escape sequence allows you to place special characters in a string. Browsers that support JavaScript can use the escape function.
To add the next line within the same string, we can use
. Let's take a look at the following code:
alert("Welcome to JavaScript. We hope you love it!")
To add quotes within a string, we can use something like this:
var myquote="Jinnah said "Impossible is a word unknown to me.""
This Unicode escape sequence starts with a backslash () followed by characters. This backslash is the escape sequence character. To insert a backslash itself, just add another backslash "
" before the next one. Also called double backslash (
\
).
You can specify a Unicode character by uaaaa
, where aaaa
is a hexadecimal number. So a Unicode escape character can represent a 16-bit character.
A Unicode escape sequence represents a Unicode sequence, as follows:
)u
)0-9
)(a-f
)For example, the word cat will be represented as u732b
.
A Unicode escape sequence can also be used in comments and literals. Consider the following example:
var x = "http\u00253A\u00252F\u00252Fexample.com";
You can also represent an escape sequence to represent a Unicode character ["u03b1
"] as ["a
"].The Unicode escape for the character é
, for example, is u00E9
, and the following two JavaScript strings are identical:
"cafn" === "cafu00e9" // => true
The ==
operator tries to convert the values to the same type before testing if they're the same; whereas, ===
does not do this. It requires objects to be of the same type to be equal.
Here are some examples of the escape sequence for some characters:
Unicode |
Escape sequence |
Meaning |
---|---|---|
|
|
Vertical tab |
|
|
New line |
|
|
White spaces |
|
|
Form field |
|
|
Line terminator |
|
Whitespace |
The Unicode standard (which can be found at http://unicode.org/standard/standard.html) defines the most acceptable pattern of encoding characters and the normalization method. Javascript presupposes that the code it is reading and interpreting has all the Unicode representations normalized. ECMAScript V6 has a string prototype function (string.prototype.normalize()
) to fix any unaddressed encodings.
Identifiers are used to give names to functions and variables. There are certain rules for these identifiers and these rules are the same for all programming languages. Identifiers can contain letters and a digest of the complete Unicode character set. These JavaScript identifiers include variables, objects, functions, properties, methods, events, and so on.
Here are the rules for identifiers:
$
)We can also use Unicode characters as identifiers. Unicode escape sequences such as uaaaa
, can also be used as identifiers. We must avoid using global methods or properties as identifiers.
A best practice while writing identifiers is to use one word and use camel case. Consider the following example:
The var
variable is a valid identifier name, but it is an invalid identifier because of the reserved word. If you use a valid identifier, then your browser will handle it correctly; however, if you use an invalid identifier, your browser will show a warning recognizing it as a bug.
Reserved words are keywords in JavaScript. There are some words that cannot be used as a function or a variable name. These reserved words are also identifiers. These reserved words are reserved for JavaScript engine. If you use these reserved words as a function, variable, or a method name, then your browser will show a warning and your script may fail.
Reserved words are basically keywords that have special meaning in JavaScript, for example, break
, case
, do
, delete
, else
, and so on.
There are three types of reserved words:
Protected reserved words cannot be used as a variable or a function name. If we use these, then there will be a compilation error in it. Here are a few protected reserved words:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JavaScript also released new keywords, and these reserved words have a special meaning for the current version of JavaScript. These reserved keywords can also be used as identifiers. Once you declare it as an identifier, then it will forget that it was a keyword. Here are some examples of new reserved keywords:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future reserved words are proposed for future extensions of JavaScript. These are also used as an identifier in the current version of JavaScript. When you choose a word as an identifier, it is also important to note whether it is already the name of JavaScript reserved keyword. Here are some examples of future reserved words:
|
|
|
|
|
|
|
|
|
|
|
|
In any programming language, comments are used to make your code readable. In JavaScript, comments are used to explain JavaScript code.
Another use of comments is to write them to add hints, suggestions, and warnings into our JavaScript code, so if someone other than the developer wants to change or modify your script, they can easily modify it. Use of comments in a script is considered as a best practice. Comments are also used to disable the execution of some parts of code. In debugging, comments are very helpful, so it is a valuable debugging tool for developers.
There are three different types of comments you can put into your JavaScript script:
We can write multiple lines between the comment boundaries; this block of code will not be executed:
/* this is a comment */
Adding two forward slashes in the beginning of the line comments it. The text between //
and the end of the line is commented and will not be compiled. Double forward slash is used to comment one line at a time:
// this is comment
JavaScript treats the HTML comment's opening sequence, that is, <!-- as // comment
. It does not recognize the closing sequence of HTML comment, that is, -->
HTML styled comments are not usually used within JavaScript code blocks as //
is much handy. It is recommended not to use HTML comments in JS as that was an old practice. They were used to hide incompatible JavaScript code from old browsers.
Literals are used to represent values for the different data structures in JavaScript; for example:
3.14
to be or not to be
In the following sections, we will cover the literal types for JavaScript.
An object literal is used to hold values in an object.
An object literal holds pairs of comma-separated lists of property names and associated values, and they are enclosed within {}
. It is possible for an object literal to have no values as well. In an object literal, each name:value
pair is separated by a comma.
Here is an example of an object literal:
varbookObj={ bookName="The Kite Runner", CoverImage="KiteRunner1.png", Author="KhaledHoesenni", bookCategory ="Bestseller" };
An array literal contains a list of expressions that represent an array element.
Array literals contain values inside an array. They are placed inside array brackets []
and each element is separated by a comma (,
). Just like an object literal, an array can be empty and contain 0
elements. When you create an array literal it is specified values as its element and its length is specified as a number of arguments of this array. Here is an example of an array literal:
var Planets= ["Earth", "Neptune", "Saturn", "Mars"];
Integer literals must contain values that are only integers.
A floating point literal is used to contain floating numbers—numbers that have a decimal point, fraction, or exponent.
String literals, as the name suggests, hold only string values, that is, values placed inside a pair of quotation marks.
A string literal contains characters placed inside quotation marks. You can also create a null string to not contain anything. Two or more strings can be joined together using the +
sign. Special characters can be inserted in the string. A few special characters are
,
, ,
, to name a few.
A few examples of string examples are as follows:
strFirstName= "Jamie"; strLastName="Down"; strMarksObtained="200"; strClassesJoined="Microprocessors, "+ "Microcontrollers"; strAddress= "Michigan University United States";
JavaScript also supports a set of statements used to identify a set of instructions and carry out tasks. There are different types of statements in JavaScript such as if
, for
, while
, and so on. The basic purpose of using these statements is to evaluate or check whether certain conditions are being met. These statements are common for all programming languages.
As the name suggests, conditional statements are based on conditions and the if
and then
rule. For example, "if you work hard, you will get rewarded". Here, working hard is the condition of getting rewarded.
Consider the following example:
if (age < 20 && age > 12) { person = "Teen ager"; } else { person = "Not a Teenager"; }
In a loop statement, a condition is provided within a set of parentheses. If the condition evaluates to true
, then the loop will continue working, and if the condition equates to false
, the program will jump to the next line after the loop. An exception is the do while
loop, which will always execute at least once. There are different types of loops in programming languages; they are as follows:
for
/in
loopswhile
loopsdo/while
loopsHere is an example that shows how the while
loop is used:
var number = ""; var n = 0; while (n < 10) { number += " The number is " + n; n++; } console.log(number);
Loops are covered in more detail in Chapter 8, JavaScript Expressions, Operators, Statements, and Arrays.
Object manipulation statements in JavaScript are used to access the object's properties and methods at runtime to perform certain functions. JavaScript uses the for…in
and with
statements to manipulate objects.
Here is an example that shows how the object car can be manipulated:
<script type="text/javascript"> var x; var car = new Object(); car.brand = "Porche"; car.model = "2014"; car.colour = "Black"; for (x in car) { console,log(x + " --- " + car[x] + "<br />"); } </script>
Output:
brand — Porche model — 2014 color — Black
The try
, catch
, throw
, and finally
statements are used in JavaScript exception handling. Let's take a look at the following description:
try
statement tests code for errors.catch
statement usually follows the try
statement and catches and handles the error found in the try
block.throw
statement creates custom errors or alerts.finally
statement executes code after try
and catch
, regardless of the result. Usually, this block of code is used to clean up and release resources.Here is an example showing how exceptions are handled using the try
and catch
block:
<!DOCTYPE html> <html> <head> <script type="text/javascript"> <!-- function Calculate() { x = document.getElementById("Val1").value; y = document.getElementById("Val2").value; try{ if ( x == 0 ||y==0 ) { throw("Divide by zero error." ); } else { var a = x / y; alert("The answer is: " + a ); } catch ( err ) { alert("Error: " + err ); } } //--> </script> </head> <body> <form> <p>Enter first value</p> <input id="Val1" type="text"> <p>Enter second value</p> <input id="Val2" type="text"> <p><input type="button" value="Calculate" onclick="Calculate();" /></p> </form> </body> </html>
Semicolons in JavaScript are used to separate statements from each other. Let's say, if you have two different statements and you just write them, then it will not be understandable. So, JavaScript uses semicolons to separate statements. It will make your code clear and meaningful. If you do not use semicolon in JavaScript, then it will make your statements complex, meaning that the end of one of your statements could be the start of your statement.
JavaScript does not consider a line break as a semicolon. Here is an example of the use of semicolons in JavaScript:
a=10; b=20; a=10; b=20;
JavaScript automatically inserts a semicolon at the end of your statement. You should not put a semicolon after the closing curly bracket. Similarly, at times, putting a semicolon after round brackets ()
is also a bad idea, and importantly, the code will not run.
If there is a for
loop in your script, then put a semicolon after the first and second statements and don't put a semicolon after third statement. It will produce a syntax error. For example, if you have a for
loop like this:
for(i=0;i<5;i++;){}
The preceding code will give you a syntax error. The right way of doing this is as follows:
for(i=0;i<5;i++){}
Here are some rules for semicolons:
3.141.42.116