Solidity vs. JavaScript: Similarities & Differences
Solidity and JavaScript share similarities in syntax, but differ in version control, type declaration, and use of \this\ keyword. Solidity has static typing and supports tuples.
Before we dive into Solidity Coding Tutorials, letโs study a Smart Contract! ๐ฌ
Here is an example smart contract called OnOffSwitch written in Solidity:
Coming from learning JavaScript, what are some of the immediately noticeable things about this programming language?
First, letโs look at the familiar! Things that are similar to JavaScript:
- Comments appear to be the same! Both syntaxes:
// commentand/* comment */ - The casing used is the same as JavaScript, it is lowerCamelCase
- Curly Braces
{}seem to serve a similar purpose, marking scope - Boolean values
true/falsewhich can be modified with boolean operators:! - The
contractkeyword seems a bit like JavaScriptclass, especially the constructor - The function syntax looks a bit similar to JavaScript
- The
returnkeyword is still used for passing a value back from a function
Now letโs look at the dissimilarities:
- There seems to be some kind of version control statement at the top:
pragma - There are public/private keywords for variables and functions
- The
isOnvariable is prefaced with its typebool - The code refers to the
isOnmember variable without usingthis - The function
toggledefines what it will return, abool
Ok, well that was a good quick glance! ๐
Now letโs dive into all of these things we noticed to learn more about the language. ๐ง
Compiler Version Control โ๏ธ
Version Control can be very helpful to specify a range of versions that are acceptable for a dependency or a tool.
In Solidity, we can specify what versions of the compiler our contract will work with using the pragma keyword:
The ^ symbol indicates that this contract will compile with not only version 0.6.2, but also anything above that version all the way up until the next minor version 0.7.0.
This syntax may look very similar to you from npm when we worked with the package.json! Both of these systems use semantic versioning where there are three values:
- The major version:
x.0.0 - The minor version:
0.x.0 - The patch version:
0.0.x
Major updates make no guarantee of backwards compatibility. Generally, major updates introduce many breaking changes that will require you to make changes to your code to update successfully.
Minor version updates will generally add functionality in a backwards-compatible way.
Patch version updates are meant for bug fixes and should not break your codeโs expected behavior (unless you were depending on behavior which was the result of a bug).
It should be noted that, at the time of writing, Solidity has yet to release its first major version. Prior to the major version 1.0.0, many systems are considered unstable where anything may change at any time. So far Solidity development has stuck to making breaking changes on the minor version updates. That is, for example, contracts written for 0.4.x may not work with solidity compiler versions 0.5.x and above. Each release will document the breaking changes.
The Contract ๐
At first glance, the contract keyword looks a bit like class in JavaScript!
๐๐ผ Here we are declaring isOn as a member variable of the OnOffSwitch contract. In Solidity these variables are generally referred to as state variables.
Just like in JavaScript classes, the constructor is run only once. For contracts, the constructor is run when it is deployed. The isOn state variable will be set to true on the deployment of this contract.
The isOn variable is accessible anywhere in this contract by name. Unlike JavaScript class variables, there is no need to use this. inside of the contract itself to gain access to the state variables.
The this keyword is still used in Solidity as a reference to the contract account. Weโll talk about this a bit more in the upcoming coding tutorials!
Since state variables are referred to by name, you may often see constructor arguments using underscores to disambiguate:
Itโs important to recognize that when we make a change to a state variable on a deployed smart contract, we are modifying permanent storage on the blockchain.
Remember from our lessons on Ethereum that permanent storage on the blockchain is stored in Patricia Merkle Tries on every Ethereum Full Node. ๐ฒ ๐ป
Local variables defined inside of a code block {} or passed in as arguments live in memory only for the length of their particular scope.
Control Structures ๐
As you may have noticed in our initial example, Solidity also has the return statement for passing back values from a function.
One difference in Solidity is that multiple values can be returned from a Solidity function as a tuple:
The following statement is perfectly valid in Solidity. Similarly, tuples can be used to destructure assignments similar to destructuring in JavaScript:
You can think of a tuple simply as a group of values in parenthesis. They are not a formal structure in Solidity so they are primarily used for returning and destructuring as shown above.
Along with the return keyword, Solidity also has if, else, while, do, for, break, and continue with the same semantics as JavaScript.
Visibility ๐
You may have noticed the keywords public and private in the initial contract example shown. These keywords are called visibility specifiers because they determine from where functions can be accessed.
As you might expect, a public function is one that can be accessed from anywhere. A private function is one that cannot be. When a variable is declared public, a getter function is generated that will allow access to the variable state.
The keyword private does not protect the privacy of the data itself. Any data committed to the Ethereum blockchain is public for anyone to see! Marking it as private will simply disallow any other contract from reading or modifying the information. ๐
In addition to public and private, there are also internal and external visibility specifiers. Weโll discuss these specifiers further as we dive into contract communication through message passing.
Static Typing ๐ก
The last big distinction we noticed from the example is that Solidity has static typing.
In JavaScript, you donโt need to specify the data type. We use keywords like var, let and const that could hold numbers, strings, and other various objects:
๐๐ผ Changing a variableโs type like this may not be the best practice, however from the JavaScript languageโs perspective, this is perfectly valid! This is because JavaScript is a dynamically typed language.
In Solidity, all variables must declare their type:
๐๐ผ Here our isOn variable is a boolean value. It must always be true or false.
By default, boolean values are false.
What if we tried storing a number in a bool? ๐ค
No good!. In fact, Solidity wonโt even compile with a statement like this. โ
The compiler will raise a โTypeError: Type int_const 10 is not implicitly convertible to expected type bool.โ And rightly so! ๐คจ
An exception raised at compile time is called a compile-time exception. This means that the compiler was unable to generate bytecode from the program, so we would not even be able to deploy this contract! This is opposed to a run-time exception which would happen when someone tried to interact with a contract on the blockchain in some expected way.
The exception would occur when a miner tries to validate the transaction. Unless the exception is caught, the transaction will fail and the miner will consume all the gas. โฝ๏ธ
Static typing also affects the way the function is declared. Letโs use our example above where we returned a tuple:
๐๐ผ Here the function must declare what type of values it is going to return. It defines that an integer and boolean will be returned in a tuple list in that order.
If it were to try and return values of a different data type it would throw an exception.
๐ Wrap Up
In this article, we went over some of the basic differences and similarities between Solidity and JavaScript! ๐
We talked about version control, contract structure, control structures, scoping, access control and static typing. Yikes, thatโs quite a lot! ๐
Donโt fret if it hasnโt all sunk in just yet, thereโs plenty of time to learn as we go through the coding tutorials. ๐ฎโ๐จ
Next up, we start coding Solidity! ๐จโ๐ป๐ฉโ๐ป
Learn More About Solidity
Alchemy University offers free web3 development bootcamps that explain the difference between Javascript and Solidity and help developers master the fundamentals of web3 technology. Sign up for free, and start building today!