Learn Solidity: Basics of Solidity By Example

Solidity is the primary programming language used to write smart contracts on Ethereum and other EVM-compatible blockchains. While its syntax is similar to JavaScript, Solidity introduces unique concepts such as gas costs, immutability, and blockchain-based execution.
This guide explains Solidity basics using a modern example contract. It updates outdated concepts, aligns with Solidity 0.8.x standards, and provides a clear understanding of how smart contracts work in practice. Start learning Solidity with real-world examples including variables, functions, and contract deployment by building expertise through a Certified Blockchain Expert, testing logic using a Python certification, and showcasing your projects via a Digital marketing course.

What Is Solidity?
Solidity is a statically typed, contract-oriented programming language used to develop decentralized applications (DApps). It runs on the Ethereum Virtual Machine (EVM), enabling developers to create secure and automated programs.
Smart contracts written in Solidity are immutable once deployed, meaning they cannot be easily changed. This makes correctness and security critical.
A Simple Solidity Contract Example
Below is a modern version of a basic token-like contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract SimpleToken {
address public owner;
uint256 public totalSupply;
mapping(address => uint256) public balances;
event Transfer(address indexed from, address indexed to, uint256 value);
event Mint(address indexed to, uint256 value);
constructor(uint256 initialSupply) {
owner = msg.sender;
totalSupply = initialSupply;
balances[msg.sender] = initialSupply;
}
function transfer(address to, uint256 amount) public {
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
balances[to] += amount;
emit Transfer(msg.sender, to, amount);
}
function mint(address to, uint256 amount) public {
require(msg.sender == owner, "Only owner can mint");
totalSupply += amount;
balances[to] += amount;
emit Mint(to, amount);
}
}
This contract demonstrates core Solidity concepts used in real-world development.
Contract Step by Step
Pragma Directive
pragma solidity ^0.8.20;
This specifies the compiler version. Modern Solidity uses version 0.8.x, which includes built-in overflow checks and improved safety.
Contract Declaration
contract SimpleToken
A contract is similar to a class in object-oriented programming. It defines state variables and functions.
State Variables
address public owner;
uint256 public totalSupply;
State variables store data on the blockchain. Declaring them as public automatically creates getter functions.
Mapping
mapping(address => uint256) public balances;
A mapping stores key-value pairs. Here, it tracks how many tokens each address owns.
Events
event Transfer(address indexed from, address indexed to, uint256 value);
event Mint(address indexed to, uint256 value);
Events allow contracts to log important actions. External applications can listen to these logs.
Constructor
constructor(uint256 initialSupply)
The constructor runs once when the contract is deployed. It initializes the owner and assigns the initial token supply.
Transfer Function
function transfer(address to, uint256 amount)
This function allows users to send tokens. It checks balances, updates them, and emits an event.
Mint Function
function mint(address to, uint256 amount)
This function creates new tokens. Only the owner can call it, ensuring controlled supply.
Key Concepts in Solidity
msg.sender
Represents the address that calls a function. It is used for authentication and access control.
require Statements
Used to validate conditions. If the condition fails, the transaction is reverted.
Gas and Execution
Every operation costs gas. Efficient code reduces transaction costs.
Immutability
Once deployed, smart contracts cannot be modified. Updates require deploying new contracts.
Differences Between Old and Modern Solidity
Older Solidity versions (like 0.4.x) required manual handling of overflows and used deprecated functions such as selfdestruct without safeguards.
Modern Solidity (0.8.x):
Includes built-in overflow protection
Improves error handling
Provides better security features
Encourages safer coding patterns
Developers should always use the latest stable version.
Best Practices for Writing Solidity Contracts
Use the latest compiler version
Follow security guidelines and audit your code
Use libraries like OpenZeppelin
Avoid unnecessary complexity
Write comprehensive tests
These practices reduce vulnerabilities and improve reliability.
Common Mistakes Beginners Make
Ignoring gas optimization
Not validating inputs properly
Using outdated Solidity syntax
Failing to test edge cases
Mismanaging access control
Avoiding these mistakes is essential for building secure applications.
Tools for Solidity Development
Remix IDE for quick testing
Hardhat for development and deployment
Foundry for fast testing and scripting
Ethers.js for frontend integration
Using these tools improves efficiency and workflow.
What to Build After Learning Basics
Once you understand Solidity basics, try building:
ERC-20 or ERC-721 tokens
Decentralized voting systems
Crowdfunding platforms
DAO governance contracts
Practical projects help reinforce concepts.
The Future of Solidity Development
Solidity continues to evolve alongside Ethereum. Trends include:
Layer 2 deployment for lower costs
Account abstraction for better user experience
Integration with zero-knowledge proofs
Increased focus on security and audits
Developers who stay updated will remain competitive in the Web3 ecosystem.
Build your first smart contracts and understand Solidity fundamentals step-by-step by mastering blockchain development through a Certified Blockchain Expert, integrating applications using a Node JS Course, and promoting your developer journey using an AI powered marketing course.
Frequently Asked Questions (FAQs)
1. What is Solidity used for?
Solidity is used to write smart contracts for Ethereum and other EVM-compatible blockchains. These contracts power decentralized applications.
2. Is Solidity similar to JavaScript?
Yes, its syntax is similar, but Solidity includes blockchain-specific features like gas costs and immutability.
3. What is a smart contract?
A smart contract is a self-executing program stored on a blockchain. It runs automatically when conditions are met.
4. What is pragma in Solidity?
Pragma specifies the compiler version required to compile the contract.
5. What is a mapping in Solidity?
A mapping is a data structure that stores key-value pairs, commonly used for balances or ownership.
6. What are events in Solidity?
Events log data on the blockchain, allowing external applications to track contract activity.
7. What is msg.sender?
It is the address of the account that calls a function.
8. What does it require to do in Solidity?
Require checks conditions and reverts the transaction if the condition is not met.
9. What is gas in Ethereum?
Gas is the fee required to execute transactions or smart contract operations.
10. What is a constructor in Solidity?
A constructor is a special function that runs once during contract deployment.
11. What is the latest Solidity version?
As of 2026, Solidity 0.8.x is widely used and recommended.
12. Can smart contracts be modified?
No, once deployed, they are immutable. Updates require deploying a new contract.
13. What is OpenZeppelin?
OpenZeppelin provides secure and reusable smart contract libraries.
14. What is ERC-20?
ERC-20 is a standard for creating fungible tokens on Ethereum.
15. What is ERC-721?
ERC-721 is a standard for creating non-fungible tokens (NFTs).
16. How do I test Solidity contracts?
You can use tools like Hardhat or Foundry to write automated tests.
17. What is Remix IDE?
Remix is a browser-based tool for writing, testing, and deploying smart contracts.
18. What are common Solidity vulnerabilities?
Common issues include reentrancy, overflow bugs, and improper access control.
19. Is Solidity hard to learn?
It is relatively easy for developers with programming experience, but mastering security takes time.
20. What should I build after learning Solidity?
Start with tokens, voting systems, or crowdfunding apps to gain practical experience.
Related Articles
View AllSolidity
Solidity Design Patterns for Web3 Apps: Access Control, Pausability, and Modular Architecture
Learn Solidity design patterns for access control, pausability, and modular architecture, including RBAC, governance admin, and upgrade-safe Web3 contract design.
Solidity
Writing ERC-20 and ERC-721 Tokens in Solidity: Standards, Extensions, and Real-World Pitfalls
Learn how to write ERC-20 and ERC-721 tokens in Solidity with key extensions like permit and royalties, plus real-world pitfalls in security, gas, UX, and compliance.
Solidity
Solidity Events and Logs Explained: Building Indexable On-Chain Analytics for dApps
Learn how Solidity events and logs work, how indexed topics enable fast off-chain queries, and how to design event schemas for scalable dApp analytics.
Trending Articles
Top 5 DeFi Platforms
Explore the leading decentralized finance platforms and what makes each one unique in the evolving DeFi landscape.
How Blockchain Secures AI Data
Understand how blockchain technology is being applied to protect the integrity and security of AI training data.
What is AWS? A Beginner's Guide to Cloud Computing
Everything you need to know about Amazon Web Services, cloud computing fundamentals, and career opportunities.