A programming language is a language used to give instructions to a computer. You write the instructions as text called source code, and a program such as an interpreter or compiler turns that text into actions the machine carries out.
What a programming language is made of
Every programming language has two parts. The first is syntax: the exact rules for how the text must be written, like where brackets go and which words are reserved. The second is semantics: what each valid piece of text means when it runs. A computer is strict about both. A missing colon or a misspelled keyword stops the program before it starts.
The vocabulary is small compared with a human language. The MDN introduction to JavaScript describes the language as having "a standard library of objects, such as Array, Map, and Math, and a core set of language elements such as operators, control structures, and statements." Python's glossary defines a statement the same way: "either an expression or one of several constructs with a keyword, such as if, while or for."
Most languages offer the same basic building blocks, with different spelling:
- Values and variables to hold data. See what a variable is in programming.
- Conditions to choose between actions, such as
if. - Loops to repeat an action, such as
forandwhile. - Functions to name a block of instructions and reuse it. See what a function is in programming.
A first instruction in two languages
The smallest useful instruction is "show this text". In Python:
print("Hello, world")
In JavaScript, run in a browser's developer console or in Node.js:
console.log("Hello, world");
Both lines do the same thing. They ask the computer to display the text Hello, world. The difference is only in the words and punctuation each language requires, which is why learning a second language is usually faster than learning the first.
A slightly longer example uses a variable, a loop and a condition, in Python:
total = 0
for number in range(1, 6):
if number % 2 == 1:
total = total + number
print(total)
Example output: 9. The loop visits 1, 2, 3, 4 and 5. The condition number % 2 == 1 is true only for odd numbers, so the program adds 1, 3 and 5, which makes 9. You can check the arithmetic by hand, and that is the point: the instructions are precise enough that the result is the same every time.
How the computer follows the instructions
A processor does not run Python or JavaScript text. It executes machine code, a sequence of binary instructions specific to the processor family. Something has to bridge the gap, and languages do it in different ways.
| Approach | What happens | Typical example |
|---|---|---|
| Compiled ahead of time | A compiler translates the whole program into an executable file before you run it. | C, Go, Rust |
| Interpreted | A program called an interpreter reads the source and carries it out. | Python, JavaScript |
| Both, in stages | Source is first compiled to an intermediate form, which a virtual machine then executes. | CPython, Java |
The Python glossary explains where Python sits. It says "Python is an interpreted language, as opposed to a compiled one, though the distinction can be blurry because of the presence of the bytecode compiler." In the reference implementation, CPython, "Python source code is compiled into bytecode", and that bytecode "is said to run on a virtual machine". The glossary also notes that "source files can be run directly without explicitly creating an executable", which is why you can type a script and run it at once. Interpreted languages "typically have a shorter development/debug cycle than compiled ones, though their programs generally also run more slowly."
So "interpreted" and "compiled" describe how a particular implementation works more than the language itself. Treat the table as a rule of thumb.
How to run your first instructions
Python has two modes. The first is interactive. Start the interpreter and it shows a >>> prompt, reads one statement at a time and runs it. On Windows with the py launcher installed, the command is py. Elsewhere it is usually python3. The Python documentation says that when you type an end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt, the interpreter exits, and that quit() also works.
The second mode runs a file. The documentation says that when the interpreter is "called with a file name argument or with a file as standard input, it reads and executes a script from that file." Save the loop example above as odd_total.py and run it:
python3 odd_total.py
Example output: 9. On Windows, use py odd_total.py if python3 is not found.
For JavaScript you do not need to install anything to start. Open a browser, open its developer tools, and type into the console. MDN describes JavaScript as "a cross-platform, object-oriented scripting language used to make webpages interactive", and it can also run outside the browser in engines such as Node.js.
Are all languages for giving instructions the same kind?
No. Languages are built for different jobs, and a few categories help when you read about them.
- General-purpose languages such as Python, JavaScript, Java and C can build many kinds of software: web servers, desktop programs, tools and games.
- Domain-specific languages are built for one task. SQL, for example, is for working with data in databases. It is declarative: you describe the result you want, not the steps. The article Is SQL a programming language? covers where it fits.
- Markup languages such as HTML describe structure and content. They tell a browser what a page contains, and do not describe steps to carry out, so they are usually kept apart from programming languages.
Languages are also standardized to different degrees. JavaScript, according to MDN, is standardized at Ecma International under the name ECMAScript, which "behaves the same way in all applications that support the standard." The standard is ECMA-262, which MDN says is also approved by ISO. A standard lets several independent engines, such as SpiderMonkey in Firefox and V8 in Chrome, run the same program the same way.
Why there are so many languages
Each language makes a different trade. Some give you direct control of memory and speed, and ask you to manage more details. Some hide those details so you can write less code and find mistakes sooner. Some are shaped around one environment, like JavaScript in the browser. None is best at everything, which is why working programmers often use several.
When you pick a first language, choose by what you want to build. For web pages, start with JavaScript. For scripting, data work or learning the concepts, Python is a common choice. The ideas you learn first, variables, conditions, loops and functions, carry across all of them.
Frequently asked questions
- Is HTML a language used to give instructions to a computer?
- HTML is a markup language: it describes the structure of a web page, such as headings, paragraphs and links. It does not express steps to carry out, so it is usually not called a programming language. JavaScript is the language that runs on the page and gives the browser instructions.
- Can a computer understand a programming language directly?
- A processor executes machine code, which is binary. Source code written in a language such as Python or JavaScript has to be translated, or run by another program that reads it, before the computer can act on it.
- What is the easiest programming language to start with?
- There is no single answer. Python is a common first choice because its syntax is short and you can try statements one at a time in the interactive interpreter. JavaScript is also a common start because every web browser can run it.
- What is the difference between a programming language and a scripting language?
- There is no hard line. MDN calls JavaScript a scripting language, and it is also a standardized programming language. 'Scripting' usually describes a language used to automate or control another program, such as a browser, and does not mean less capable.