Skip to content

JavaScript Syntax and Basics

JavaScript is a versatile and dynamic programming language, but before diving into complex topics, it’s essential to understand its structure and syntax. In this post, we’ll discuss case sensitivity, semicolons, indentation, variable declarations, and primitive data types. Let’s get started!

Case Sensitivity, Semicolons, and Indentation

Section titled “Case Sensitivity, Semicolons, and Indentation”

JavaScript, like many other programming languages, has a specific set of rules around syntax. Adhering to these rules is crucial for writing clean and functional code.

JavaScript is case-sensitive, which means variables, function names, or any identifiers with different cases are considered distinct. Be careful with how you name your variables and functions:

let firstName = "John";
let firstname = "Doe";
console.log(firstName); // Outputs: John
console.log(firstname); // Outputs: Doe

Though they look similar, firstName and firstname are interpreted as two separate variables due to case sensitivity.

In JavaScript, semicolons are used to terminate statements. While they are optional in most cases due to automatic semicolon insertion (ASI), it’s a good practice to include them to avoid unintended bugs.

let x = 5; // Recommended
let y = 10; // Though the second line works, avoid leaving out semicolons to ensure clarity.

Proper indentation enhances the readability of your code, making it easier to debug and collaborate. Most JavaScript developers use 2 or 4 spaces per level of indentation. For example:

function greet(name) {
if (name) {
console.log(`Hello, ${name}!`);
} else {
console.log("Hello!");
}
}

Remember, consistent indentation improves the structure of your code, ensuring maintainability.


Declaring Variables with var, let, and const

Section titled “Declaring Variables with var, let, and const”

Variables are used to store data. In JavaScript, you can declare variables using three keywords: var, let, and const.

var is the old way of declaring variables in JavaScript. It has function scope, meaning it is visible throughout the function in which it is declared, regardless of block scope. It can also be redeclared and reassigned.

var name = "John";
console.log(name); // Outputs: John
var name = "Doe"; // Redeclaration allowed console.log(name); // Outputs: Doe

However, modern JavaScript discourages using var due to its scope limitations and tendency to cause bugs.

let is block-scoped, which means it is limited to the block (or curly braces) in which it is declared. It is a safer and more modern way to declare variables that can be reassigned.

let age = 25;
console.log(age); // Outputs: 25
age = 26; // Reassignment allowed console.log(age); // Outputs: 26

const is also block-scoped but is used to declare variables that cannot be reassigned. It enforces immutability for primitive types.

const pi = 3.14;
console.log(pi); // Outputs: 3.14
// pi = 3.14159; // This will throw an error: Assignment to constant variable

When declaring constants, always initialize them at the time of declaration since they cannot be left undefined.

  • Use const by default unless you know the variable’s value will change.
  • Use let only when reassignment is required.
  • Avoid var in modern JavaScript development.

There are seven primitive data types in JavaScript. These types represent simple, immutable values and are the building blocks of the language.

Strings are sequences of characters, enclosed within single ('), double ("), or backticks (`).

let singleQuote = "Hello";
let doubleQuote = "World";
let templateLiteral = `Hello, ${doubleQuote}!`; // String interpolation
console.log(templateLiteral); // Outputs: Hello, World!

The number type includes integers and floating-point numbers.

let age = 30; // Integer let price = 19.99; // Floating-point number
console.log(age + price); // Outputs: 49.99

JavaScript doesn’t differentiate between integers and floats; both are treated as number.

Booleans represent true or false and are often used for conditional logic.

let isActive = true;
let isComplete = false;
console.log(isActive && isComplete); // Outputs: false

null represents the intentional absence of a value.

let result = null;
console.log(result); // Outputs: null

undefined means a variable has been declared but not yet assigned a value.

let value;
console.log(value); // Outputs: undefined

Introduced in ES6, symbol represents a unique and immutable value, often used for object keys.

const uniqueKey = Symbol("key");
console.log(uniqueKey); // Outputs: Symbol(key)
TypeExampleDescription
string'Hello'Text or character data
number42, 3.14Numeric values
booleantrue, falseLogical values
nullnullIntentional absence of value
undefinedundefinedDefault value for uninitialized variables
symbolSymbol()Unique, immutable value