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.
Case Sensitivity
Section titled “Case Sensitivity”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: Johnconsole.log(firstname); // Outputs: Doe
Though they look similar, firstName
and firstname
are interpreted as two separate variables due to case sensitivity.
Semicolons
Section titled “Semicolons”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; // Recommendedlet y = 10; // Though the second line works, avoid leaving out semicolons to ensure clarity.
Indentation
Section titled “Indentation”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: Johnvar 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: 25age = 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.
Best Practices
Section titled “Best Practices”- 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.
Primitive Data Types in JavaScript
Section titled “Primitive Data Types in JavaScript”There are seven primitive data types in JavaScript. These types represent simple, immutable values and are the building blocks of the language.
1. string
Section titled “1. string”Strings are sequences of characters, enclosed within single ('
), double ("
), or backticks (`
).
let singleQuote = "Hello";let doubleQuote = "World";let templateLiteral = `Hello, ${doubleQuote}!`; // String interpolationconsole.log(templateLiteral); // Outputs: Hello, World!
2. number
Section titled “2. number”The number
type includes integers and floating-point numbers.
let age = 30; // Integer let price = 19.99; // Floating-point numberconsole.log(age + price); // Outputs: 49.99
JavaScript doesn’t differentiate between integers and floats; both are treated as number
.
3. boolean
Section titled “3. boolean”Booleans represent true
or false
and are often used for conditional logic.
let isActive = true;let isComplete = false;console.log(isActive && isComplete); // Outputs: false
4. null
Section titled “4. null”null
represents the intentional absence of a value.
let result = null;console.log(result); // Outputs: null
5. undefined
Section titled “5. undefined”undefined
means a variable has been declared but not yet assigned a value.
let value;console.log(value); // Outputs: undefined
6. symbol
Section titled “6. symbol”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)
Summary Table of Primitive Types
Section titled “Summary Table of Primitive Types”Type | Example | Description |
---|---|---|
string | 'Hello' | Text or character data |
number | 42 , 3.14 | Numeric values |
boolean | true , false | Logical values |
null | null | Intentional absence of value |
undefined | undefined | Default value for uninitialized variables |
symbol | Symbol() | Unique, immutable value |