Character Set in JavaScript
In the previous blog, we learned about Introduction to JavaScript. JavaScript is the Web programming language used by the vast majority of modern websites, and all modern web browsers. If you want to know more about JavaScript, visit the JavaScript Introduction blog. In this blog, we will learn about what a Character Set is, key features of a Character Set, etc.
What is a Character Set?
A character set is a collection of text-representable characters. In JavaScript, we use a character set called Unicode. The Unicode character encoding standard is utilized by most of the world's writing systems. Every character in the Unicode character set has its own code point. A code point is a number that uniquely identifies a character. The code points are distributed in such a way that no two characters have the same code point.
Features of Character Set
Following are some of the features of Character Set
Case Sensitivity
JavaScript is picky about how you write things — it cares about capital letters! This means keywords, variables, and function names all need to have the same capitalization throughout.
For instance, if we want to check some condition using the keyword if, then we must write it as if not If or IF. Because if we write If or IF instead of the keyword if, then JavaScript will consider them as two different variables. Similarly accno, accNo, Accno, AccNo, ACCNO are considered as five different variables. But on the contrary, HTML is not case-sensitive. In HTML if we write the keyword if as if or IF both will be considered the same and will work as a control statement.
Many JavaScript objects and properties on the client side share the same names as the HTML tags and attributes they represent. While these tags and attribute names can be put in any case in HTML, but in JavaScript they must usually be all lowercase. For example, the HTML onclick event handler attribute is sometimes specified as onClick in HTML, but in JavaScript code (or in XHTML documents), it must be specified as onclick.
Here are the key points about the case sensitivity of JavaScript
Variable Names
JavaScript variables are case-sensitive. For instance, varName, VarName, varname and VARNAME are separate variable names. In the following example, all variables are treated as separate variables due to the variation in letter casing.
Output
Function Names
The names of functions are similarly case-sensitive. MyFunction is not synonymous with Myfunction or myfunction. In the following example, all Functions are treated as separate Functions due to the variation in letter casing.
Output
Keywords
JavaScript keywords like if, else, while, and for are all in lowercase. Using various case variations will not work. In JavaScript, keywords, also known as reserved words, are case-sensitive. This means that the language treats the uppercase and lowercase versions of keywords as distinct entities.
Output
In the above code as we had written If instead if on the 2nd line so the code terminated and compiler throwed an error.
Let's see the example which will be error-free using keyword if
Output
Object Properties
When accessing object properties or methods, you need to use the correct case. For instance, if an object has a property called name, attempting to access it using Name or NAME will result in an error. In the following example, all Object variables are treated as separate Object variables due to the variation in letter casing.
Output
String Comparison
String Comparison is also case-sensitive. In String Comparison if the string is hello and if we compare it with Hello then it will give us a different result .i.e hello is not considered the same as Hello.
The equality operator (==) compares values for equality after performing type coercion if needed. The strict equality operator (===) checks for both value and type equality without coercion.
Let's take a look into the example
Output
HTML Attributes
HTML properties that relate to JavaScript functions or variables are also case-sensitive. For example, <button onclick="displayMsg()">Click me</button> would invoke a function called displayMsg, not DisplayFunction.
Coding Conventions
While JavaScript is case-sensitive, coding standards frequently encourage using camelCase for variable and function names, with the first letter in lowercase (e.g., empName, displayMsg). Class names usually use PascalCase (e.g., EmployeeDetails).
String Methods
JavaScript includes many built-in methods for working with strings, including toUpperCase(), toLowerCase(), charAt(), indexOf(), substring(), and more.
White Space and Line Break
White Space
JavaScript does not support whitespaces. This means that you can remove all whitespaces from a JavaScript program and still have legal JavaScript code. When JavaScript code is minified, whitespaces are removed. Minification is the process of removing whitespaces and other unnecessary characters from JavaScript code to make it smaller.
In JavaScript, whitespaces can be used to regulate the execution flow. For example, you could utilize whitespaces to create a code block that is only run if a certain condition is met.
Let's take a look at an example
Output
Line Break
In JavaScript, there are two ways to insert a line break:
- The \n escape sequence is used.
- Using the HTML element <br>.
The \n escape sequence is a special character that signals the beginning of a new line. It can be used to produce a line break in strings. For instance, the following code will generate a string of two lines:
Let's take a look into an example
Output
In HTML, the <br> HTML element is used to produce a line break. In JavaScript, it can also be used to generate a line break. For example, the following code will generate a string with two lines:
Let's take a look at an example
Output
The method you use to produce a line break in JavaScript is determined by individual requirements. When working with strings, the \n escape sequence is an excellent choice. When working with HTML, the <br> element is a smart choice.
Here are some other things to remember regarding line breaks in JavaScript:
- The JavaScript console does not always show line breaks. This is because the console will automatically insert line breaks into large strings.
- Line breaks can be used to manipulate the layout of text on a web page.
- Line breaks can be used to separate text into paragraphs.
JavaScript's character set, Unicode, is like a toolkit for letters and symbols, forming a universal language for computers. Each character has a unique number, and using the right capitalization is key. This set is the backbone, ensuring text works seamlessly across languages in web development. In the next blog, we will learn about Unicode Escape Sequence and Normalization.