If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Our first program

AP.CSP:
AAP‑2.B.2 (EK)
,
CRD‑2.B.1 (EK)
When we write a computer program, we're giving the computer a set of instructions for what we want it to do. Just like we can tell a dog to "sit" or "beg", we can tell a computer to "add" or "print". A computer isn't as furry as a dog, but fortunately, it's a lot more reliable! And just a bit better at math...
A laptop with a collar and a leash. A speech bubble says "ADD!" on the side. The laptop displays "2 + 2" and "4".
There are many languages we can use to write computer programs. Here on Khan Academy, we'll use our live editing environment with the JavaScript language.
Let's start with a simple "Hello World" program, the first program of many new programmers. Our goal is to get the computer to display "Hello World" somewhere on the screen.
That's as simple as:
println("Hello, World");
Try typing that in the editor below:
If you make any typos or syntax mistakes along the way, our error buddy will pop up and let you know. Everyone makes mistakes while programming, so it's totally okay. That's how we learn and improve!

How does it work?

Now let's break down our 1-line program:
println("Hello, World");
That line of code is called a statement. All programs are made up of statements, and each statement is an instruction to the computer about something we need it to do.
The println() command is also called a function, method, or procedure. That line tells the computer to call the procedure named println that knows how to display output in a console.
The println() procedure expects a single parameter which specifies what text the computer should display. In this program, the value passed in is "Hello, World!", so that is what's displayed.
Display commands are different in each environment and language. The println() command works here, but it may not work in other places. Other ways to display in JavaScript include console.log() and alert(), and you'll find even more ways in other languages like Python, Java, and Snap.

🙋🏽🙋🏻‍♀️🙋🏿‍♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!

Want to join the conversation?

  • leaf grey style avatar for user Quinn Hardbrook
    Why do we use println and not text? Is it because there are multiple parameters to text?
    (6 votes)
    Default Khan Academy avatar avatar for user
    • hopper jumping style avatar for user pamela ❤
      From the author:The text() command displays text on the canvas, while println() displays text in a console. We aren't teaching any ProcessingJS drawing functions in this course, since the focus is on programming concepts common across many languages and environments. Thus, I decided to use println() for the examples.
      (2 votes)
  • piceratops seed style avatar for user John Abou Elias
    I learned PHP but it's never taught. Is PHP just too old and irrelevant nowadays?
    (4 votes)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user Gabriel Admasu
    Programming give instructions to computer by using programming languages and this instructions are changed in to 0s and 1s so how this 0s and 1s tells for computer to do something? How does it works?
    (0 votes)
    Default Khan Academy avatar avatar for user
    • leaf green style avatar for user Shane McGookey
      Diving into how the 0s and 1s define operations for the computer is an entire course in and of itself. I will try to simplify it to provide a high-level overview:

      Consider this... Once the high-level programming language instructions have been converted into binary machine code (by running through an assembler and compiler, sometimes these two are coupled) then that machine code will be loaded into the computer's main memory (RAM - Random Access Memory - also referred to as Dynamic Random Access Memory). The computer's "brain," the Central Processing Unit (CPU) will sequentially pull instructions from Main Memory (i.e. it will pull instruction #1, then instruction #2, etc. - it is a bit more complicated than this, but this is a simplifying assumption that makes this explanation easier) and will interpret (decode) and execute each instruction. Instructions are a sequence of bits, and this sequence can be broken apart to represent a specific operation.

      Consider a sequence of 16 bits that were pulled from the main memory. These 16 bits represent the first program instruction. The bit sequence might be 1110011111010000. The most-significant bits, '111' have no meaning. The seven bits following '111', i.e. '0011111' may represent that you want to add 1 to a value stored in one of your registers (a register is the smallest unit of computer memory, generally holding one word worth of data - 32 bits in a 32-bit machine or 64-bits in a 64-bit machine - and resides within the CPU itself) and then store the result somewhere. The next three bits, i.e. '010' might represent where you want to store the result, in this instance let us say you want to store it back in the same register that you pulled from before incrementing by 1. The last 3 bits, '000' may define a JUMP operation. In this instance, '000' will mean that you do not want to perform a JUMP operation. Note that a JUMP operation refers to jumping to another program instruction based on the results of your earlier operation (similar to an If-statement in a high-level language).

      Note that I have used the expression "may" many times in this explanation. I have used this expression repeatedly because the sequence of bits that represent a program instruction will be interpreted differently in different machines based on their instruction set architecture. Also, the instructions may be represented by more or less than 16-bits, I simply used 16-bits above for a simple instruction and referenced the nand2tetris material available online to define how those 16-bits might be converted to machine operations.

      The process is complex and too much for one comment, but hopefully this motivates you to explore further. I hope this was helpful!
      (8 votes)
  • duskpin ultimate style avatar for user User Name
    why can't we do other commands in the javascript box, such as 'confirm("hello world");'
    (3 votes)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user daneva6180
    will the outcome change if you put 'print' or 'printIn'
    (2 votes)
    Default Khan Academy avatar avatar for user
  • aqualine ultimate style avatar for user The Lightning Programmer
    What's the difference between println and print()?
    (2 votes)
    Default Khan Academy avatar avatar for user
    • hopper jumping style avatar for user pamela ❤
      It depends on the language and the environment.

      In the Khan Academy programming environment, we use the JavaScript language plus ProcessingJS functions.

      There's a println() function in ProcessingJS that prints out to the console, so that's what our examples use. That stands for "print line", as it always puts a new line after whatever it prints.

      Other environments may have functions named print() that print to a console. For example, Python 3 has a print() function that works similarly to the println() function here.
      https://repl.it/@PamelaFox1/PrintExample
      (2 votes)
  • blobby green style avatar for user NAVEED RIAZ
    I’m using an iPad and Can’t see the editor properly. I tried to open a new tab, but there’s no output for the command.
    (2 votes)
    Default Khan Academy avatar avatar for user
  • starky seed style avatar for user Mohamed Khalid
    yo what is the difference between Canvas and Console?
    (0 votes)
    Default Khan Academy avatar avatar for user
    • aqualine ultimate style avatar for user Martin
      The console is more bare-bones, it's the simplest way for us to interact with a computer (no graphics, but still something that can be handled by a human without too many additional skills). If you have windows you can right-click on the start symbol and click Windows PowerShell, on Linux systems you can search for the terminal app. Both are consoles.

      The canvas is something created by the program as a place for your graphics. It's a lot more complex and resource-intensive than the console. Normally creating a canvas takes a little bit more work than just working with the console, but here the programming language does most of the work for you.
      (4 votes)
  • piceratops tree style avatar for user Tausif Ahmed
    Are these commands case sensitive?
    (0 votes)
    Default Khan Academy avatar avatar for user
    • hopper jumping style avatar for user pamela ❤
      Yes, commands (procedure names) are case sensitive in JavaScript. If you use a different casing, it will either call the procedure named with that casing, or it will error because it can't find a procedure with that name. Try it and see!
      (4 votes)
  • blobby green style avatar for user Santosh Purohit
    Where is python i need python
    (1 vote)
    Default Khan Academy avatar avatar for user