The character set

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:

  • ISO-8859-1: This is used to encode the Latin alphabet
  • UTF-8: This is used for Unicode encoding that is compatible with ASCII

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">?

Note

For this, you can also change your web server's configuration to serve as UTF-8. Alternatively, we can send a content-type header to our server-side script.

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>

Note

Another way is to add the UTF-8 character set into the server configuration file (.htaccess), as follows:

AddCharset UTF-8

So, character set encoding will apply to all JavaScript pages in your application.

Case sensitivity

HTML is not a case-sensitive language, but XML and JavaScript are case sensitive languages.

Description

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();

Note

Keywords in JavaScript are all in lowercase such as while, for, if, and so on.

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

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.

Description

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 X

If 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:

  • The string.replace() function is used to replace all leading whitespaces with an empty string
  • The str.trim() function is used to remove spaces from both sides of a string

The Unicode escape sequence

Every 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.

Note

A code point (also known as character code) is a numerical representation of a specific Unicode character.

Description

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:

  • A backslash ()
  • A character (u)
  • A hexadecimal number (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";

Note

The hexadecimal part of this kind of a character escape is case-insensitive, which means u041a and u041A are equivalent.

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

u000B

v

Vertical tab

u000A

New line

u0009

White spaces

u000C

f

Form field

u000D

Line terminator

u0020

 

Whitespace

Normalization

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.

Note

ECMAScript 6 introduces String.prototype.normalize(), which takes care of Unicode normalization.

Identifiers

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:

  • Identifiers cannot start with a number
  • Identifiers can contain numbers, letters, underscore, and the dollar sign ($)
  • It can be of any length
  • These are case-sensitive
  • We must not use reserved words for identifiers

Tip

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:

  • myNote
  • myNewNotebook
  • $sum

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 keywords

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
  • New reserved word
  • Future reserved words

Protected 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:

Break

case

catch

class

const

Continue

Debugger

Default

Delete

do

Else

Export

Extends

False

If

Import

in

var

New 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:

Abstract

Boolean

byte

char

enum

final

doubke

implements

int

interface

internal

long

set

short

static

uint

ulong

ushort

Future reserved words

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:

asset

event

namespace

require

transient

violate

ensure

goto

native

synchronized

use

Invariant

Comments

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:

  • Multiple-line comments
  • One-line comments
  • The HTML comment opening sequence

Multiple-line comments

We can write multiple lines between the comment boundaries; this block of code will not be executed:

/* this is a comment */

One-line comments

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

The HTML comment opening sequence

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

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.

Object literals

An object literal is used to hold values in an object.

Description

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"
};

Array literals

An array literal contains a list of expressions that represent an array element.

Description

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"];

Boolean literals

As the name suggests, a Boolean literal has Boolean values, so its value is either true or false.

Integers

Integer literals must contain values that are only integers.

Description

In JavaScript, an integer literal must contain a digit from 0 to 9. Commas and brackets are not allowed in integer literals. Integers can be negative or positive. If there is no sign present then it is consider as positive.

Floating point literals

A floating point literal is used to contain floating numbers—numbers that have a decimal point, fraction, or exponent.

Description

Examples of floating point literals are as follows:

22.56
-26.35689
18.4e2 //Equivalent to [18.4 x 10^2] 
-17.22E3 //Equivalent to [17.22 x 10^-3]

String literals

String literals, as the name suggests, hold only string values, that is, values placed inside a pair of quotation marks.

Description

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";

Statements

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.

Conditional statements

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";
}

The loop statement

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:

  • The for loops
  • The for/in loops
  • The while loops
  • The do/while loops

Here 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

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

Exception handling statements

The try, catch, throw, and finally statements are used in JavaScript exception handling. Let's take a look at the following description:

  • The try statement tests code for errors.
  • The catch statement usually follows the try statement and catches and handles the error found in the try block.
  • The throw statement creates custom errors or alerts.
  • The 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>

Optional semicolon

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.

Note

Placing semicolons in your code can help you prevent a lot of errors. Absence of semicolons, from your code block will treat it as a single line of code when compiled resulting in several errors.

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;

Note

A semicolon in JavaScript is not a terminator; these are just separators used to separate statements.

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:

  • Insert a semicolon when an assignment operator is used
  • Insert a semicolon after rite operand of assignment operator
  • Insert a semicolon after a closing round bracket of a function
  • Insert a semicolon after keywords
  • When you declare a variable insert a semicolon
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.2.225