Code has been added to clipboard!

JavaScript Syntax Explained: Is JavaScript Case Sensitive or Not

Reading time 5 min
Published Jan 6, 2016
Updated Oct 1, 2019

Like any other programming language, JavaScript has a set of specific rules for writing code. These rules are called JavaScript syntax. Writing JavaScript code neatly and syntactically correct makes your code bug-free and easy to read.

In this JavaScript syntax tutorial, you'll find the rules for writing JavaScript code. You will learn correct capitalization of function names, variables, and other identifiers. You'll also find examples which will show you how JavaScript code should be written.

JavaScript Syntax: Main Tips

  • This is a collection of specific rules on how programs in JavaScript are constructed.
  • You should follow these rules to maintain your code neat and bug-free.
  • JavaScript statements may include any of these things: operators, values, keywords, expressions, and comments.

Syntax and Case Sensitivity

By definition, a computer program is like a list of instructions for the computer to execute. We refer to these instructions for the computer as statements. However, JavaScript gives commands to the web browser instead of the computer itself.

JavaScript statements don't need to be placed in parentheses or braces. They are, however, separated by semicolons:

Example
var a = 7;
var b = 5;
var c = a + b;

In JavaScript, identifiers are used to name variables, keywords, functions, and labels. Their naming rules are not much different from any other programming language.

To begin with, the first character of an identifier has to be either a letter, a dollar sign ($), or an underscore (_). So, for example, A, _, or $ in JavaScript are all identifiers. The following characters can be digits, letters, underscores, or dollar signs.

Note: a number cannot be the first character of an identifier.

Each JavaScript identifier is case sensitive: for example, Name and name, are not the same variable in JavaScript:

Example
Name = "Joe";
name = "John"; // Now we have two separate string variables

Note: same rules apply to keywords and other identifiers as well.

Whenever case sensitivity is relevant, it's important to have a clear standard of writing names that consist of more than one word. Throughout the history of programming, there have been three most popular methods to achieve that: hyphens (first-name), underscores (first_name), and camel case (FirstName).


Camel case is the default identifier writing method in JavaScript and many other programming languages. Keep in mind though that in JavaScript the identifier name usually begins with a lowercase letter (firstName)

Note: the hyphens method does not work in JavaScript. Hyphens are only used for substractions.

Types of Literals

Literals define fixed values used in JavaScript. There are a few types of literals we use, and they have their own rules to be followed.

Number literals, according to JavaScript syntax, can be written with or without the decimal point. This is shown in the example below:

Example
12.50
125

Note: Be sure to use a decimal point (.) and not a comma (,).

Another type of a literal is a string. It defines a piece of text within quotes. String values can be written in single or double quotes:

Example
var name1 = "value1";
var name2 = 'value2';

Note: A string does not necessarily have to have a written value and can be an empty string instead.

Boolean literals have two values: either true or false. Check out the example below:

Example
var xyz = true;  
var xyz = false;

Variables and Operators

In programming languages, the purpose of JavaScript variables is to store data. Variables are written as var in JavaScript syntax. So you need to use the var keyword when declaring variables. You can assign values to variables using an equal sign =.

In the example below, we declare a new variable a and assign a value to it:

Example
var a;
a = 10;

You should use the assignment operator (=) for assigning values to JavaScript variables. The operators should be separated with spaces from both ends, as it is good coding practice and makes it easier to read:

Example
var a = 20;

When you need to define calculations, you can also use other arithmetic operators (+, -, *, /) for computing values. These operators must also be separated with spaces, as it makes the code easy to read:

Example
(7 + 5) * 12

Expressions

An expression in JavaScript is a combination of variables, values, and operators, that is computed to a value. We refer to this computation as an evaluation. Therefore, it becomes an expression. For example, the expression 2 * 2 will evaluate to 4, like in the example below:

Example
var a = 2 * 2;

Some expressions can include variables as well. However, this is not mandatory - you don't have to include them if you don't need them.

The values can also be both variables and strings combined. For example, the expression "Joe" + " " + "Johnson", will evaluate to "Joe Johnson":

Example
"Joe" + " " + "Johnson"

Keywords and Comments

JavaScript keywords are used to identify actions to be performed by JavaScript. JavaScript keywords also help the browser to establish what action to perform. For example, the most popular keyword var tells the browser to create a new variable:

Example
var a = 7 + 5;
var b = a * 12;

Not every JavaScript statement is meant to be executed by the browser. The code written after double slashes // or in between /* and */ is referred to as a comment. This function is similar to HTML Comments.

Comments are used by programmers to leave notes for themselves and other developers to improve the readability of the code. It is especially useful in large JavaScript files, where it is easy to get lost between the statements. Also, it is helpful when other developers read your code.

Comments are ignored by the browser and not displayed:

Example
var a = 7;   // This statement is going to be executed
// var b = 5; This statement is not going to be executed. It is just a comment

JavaScript Syntax: Summary

  • JavaScript syntax is rather strict.
  • JavaScript operators must be separated with spaces from both ends.
  • Letter, _, or $ in JavaScript are identifiers. Numbers cannot be identifiers.
  • JavaScript is case sensitive. Mixed letter size can cause bugs.
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen