JavaScript Syntax and Basics

 JavaScript Syntax – The Basics You Need to Know

To start writing JavaScript effectively, it's important to understand how the language is structured. This section will walk you through the fundamentals of JavaScript syntax, including variables, data types, spacing, and more.

 Where Do You Write JavaScript Code?

As mentioned earlier, JavaScript code can be:

  • Written inside an HTML file using the <script> tag

  • Placed in a separate file with a .js extension and linked using the src attribute

<script>
    // Write JavaScript code here...
  </script>

Character Set

JavaScript uses the Unicode character set, which supports almost all characters from every language, as well as symbols, punctuation marks, and emojis. This makes it versatile and globally usable.


Case Sensitivity

JavaScript is case-sensitive, meaning:

  • name is different from Name

  • myFunction is different from MyFunction

Be consistent with how you name variables and functions to avoid unexpected errors.


 Variables

Variables store data like numbers, text, or boolean values. You can declare a variable using:

  • var (older, but still used)

  • let (preferred for block-scoped variables)

  • const (for values that shouldn’t change)

 Example:

<script>
    var name = "Steve";
    let id = 10;
  </script>

Semicolons

Semicolons (;) are used to separate statements in JavaScript. While they are optional, it's a good practice to include them for better readability and to prevent certain parsing errors.

<script>
    var one = 1;
    var two = 2;
    var three = 3;
    var four = 4;
    var five = "Five" // works even without a semicolon
  </script>

Whitespaces

JavaScript ignores extra spaces and tabs. These two lines do the same thing:

<script>
    var one = 1;
    var   one     =     1;
  </script>

Use spacing to keep your code clean and readable—it won’t affect how the code runs.


Comments

Comments help explain your code. JavaScript ignores them when running the script.

  • Single-line comment starts with //

  • Multi-line comment is wrapped in /* */

Example:

<script>
    var one = 1; // This is a single-line comment
 
    /*
      This is a multi-line comment.
      It can span multiple lines.
    */
    var two = 2;
  </script>

Strings

A string is a piece of text. You can wrap it in single or double quotes.

<script>
    var msg1 = "Hello World"; // double quotes
    var msg2 = 'Hello World'; // single quotes
  </script>

 Numbers

JavaScript handles whole numbers (integers), decimals (floats), and even hexadecimal values. Just don’t wrap numbers in quotes.

<script>
    var age = 25;
    var price = 10.99;
    var hex = 0xFF; // hexadecimal
  </script>

Booleans

Booleans represent true or false values.

<script>
    var isOnline = true;
    var hasPermission = false;
  </script>

They’re commonly used in conditionals like if statements.


Keywords (Reserved Words)

JavaScript has reserved words that you cannot use as variable names or function names. These are part of the language's syntax.

Common Reserved Keywords:

Control FlowFunctions & ObjectsValues/Operators
iffunctiontrue, false
elsereturnnull
forclassnew, typeof
whilethisvoid, delete
switchtry, catchin, instanceof