Writing your first JavaScript programme
Tags: beginners, tutorial, learning, javascript
Published: 2019-08-21 13:39:07

---
title: Writing your first JavaScript programme
published: true
tags: beginners, tutorial, learning, javascript
canonical_url: https://www.codetips.co.uk/languages/javascript/writing-your-first-javascript-programme/
---

> By the end of this article, you will have written a JavaScript programme that outputs a sentence based on a number of variables.

 ![Writing your first JavaScript programme](https://images.unsplash.com/photo-1472289065668-ce650ac443d2?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

If you've made it to this article you should have read the [JavaScript Introduction](https://www.codetips.co.uk/javascript-introduction/), and be familiar with [variables](https://www.codetips.co.uk/what-is-a-variable/) and [data-types](https://www.codetips.co.uk/what-is-a-data-type/).

JavaScript is a weakly-typed language, so its types are [inferred](https://www.codetips.co.uk/translation-and-types/). To declare a variable in JavaScript we used the `var` keyword. In the following example:

- `x` is assigned the value `"Hello World"` and has an inferred type of `string`.
- `y` is assigned the value `True` and has an inferred type of `boolean`.
- `a` is assigned the value `1` and has an inferred type of `number`.

<!--kg-card-begin: code-->
```
var x = "Hello World"
var y = true
var a = 1
```
<!--kg-card-end: code-->

**Note:** There are other ways to declare variables in JavaScript but we’re purposefully omitting them until a later article.

As JavaScript is a [weakly-typed language](https://www.codetips.co.uk/translation-and-types/), there is no way to declare a variable as a specific [data-type](https://www.codetips.co.uk/what-is-a-data-type/).

It is, however, possible to perform logic based on the type, using the `typeof` keyword.

The following [if](https://www.codetips.co.uk/what-is-an-if-statement/) statement checks to see if the type of `y` is a `boolean`. If it is, the programme will run the code within the curly brackets (`{}`), otherwise it will skip that block entirely.

<!--kg-card-begin: code-->
```
var y = true

if (typeof y === 'boolean') {
  // do something if y is a boolean
}
```
<!--kg-card-end: code-->

It is possible to have the [interpreter](https://www.codetips.co.uk/translation-and-types/) perform "typecasting" in JavaScript, but this is not something we’re going to cover in this article.

<!--kg-card-begin: hr-->
* * *
<!--kg-card-end: hr-->
## Your first JavaScript programme

It's finally time to write your first JavaScript programme. You can write this anywhere on your machine, however, it is best practice to keep your development code well organised.

Within your home directory (`$HOME` on Linux and Mac, `%userprofile%` on Windows) create a new directory called `javascript` (e.g. the full Linux path would be `/home/codetips/javascript`).

Navigate to this directory, create another directory called `firstApplication` and open it in your code editor of choice.

We're going to leave out most of the specifics of JavaScript best practices, so you're not overwhelmed with too much information, but everything will be explained in future articles.

In your new `firstApplication` directory, create a new file called `index.js`, add the below code into it, and save the file.

<!--kg-card-begin: code-->
```
console.log("Welcome to my first JavaScript programme")
```
<!--kg-card-end: code-->

Now, open a terminal, navigate to your `firstApplication` folder, and run the following command:

<!--kg-card-begin: code-->
```
node index.js

```
<!--kg-card-end: code-->


Your output should look very similar to the following:


<!--kg-card-begin: code-->
```
$ node index.js         
Welcome to my first JavaScript programme

```
<!--kg-card-end: code-->

As you can see, the `console.log` function outputs text to the terminal. Unfortunately, as a first programme goes, this is pretty boring. Let's make this more personal with [variables](https://www.codetips.co.uk/what-is-a-variable/)!

Replace the contents of `index.js` with the following code:

<!--kg-card-begin: code-->
```
var name = "Simon"
var age = 29
var profession = "Developer"

console.log(
  "Welcome to my first JavaScript programme. My name is %s. I am %d years old. I work as a %s", 
  name, age, profession
)

```
<!--kg-card-end: code-->

We’ve extended our programme to output a longer sentence with some specific information about us.

Notice in the first parameter, to `console.log` (the text beginning “Welcome to my first”), we have string substitution (`%s` and `%d`). These are effectively placeholders that we then pass in as additional arguments to `console.log` (`name`, `age` and `profession`).

The significance of the letter after the percent (`%`) sign, is it defines the type of the argument that will be passed in (`%s` denotes a string and `%d` an int).

Another way to write this is to use something called a template literal:

```
console.log(`Welcome to my first JavaScript programme. My name is ${name}. I am ${age} years old. I work as a ${profession}.`)

```

See how we’ve used backticks (`` ` ``) instead of double quotation marks (`"`), and our placeholders are now declared using a dollar sign and curly braces (`${}`) instead of string substitution.

Well done for finishing your first JavaScript programme! Now see if you can complete the following challenges:

- Change the variables so the programme prints out your name, age and profession, instead of mine.
- Add a new variable, `favoriteAnimal`, and incorporate that into your outputted sentence.

Remember you can always [reach out to us](https://www.codetips.co.uk/contact-us/) with any questions or feedback - we’d love to hear from you!

---

[CodeTips](https://www.codetips.co.uk) strives to help beginners, with zero or very little experience, learn to code.

We do cross-post to other sites to reach a wider audience, but why not [subscribe to our newsletter](https://mailchi.mp/92cccf7024b2/codetips) and get the newest articles straight to your mailbox? 

The original source for this content is [CodeTips](https://www.codetips.co.uk). The original content is kept up-to-date, but other sources may not be the latest version.