I am in the process of teaching my daughters Javascript. Also, I am periodically asked by non-programmers or friends "What should I learn?" and recently a relative, who is a school teacher, was asking for guidance so that she could help maintain her school's website. I decided to take the advice and lessons I've shared with them and share them here as well.

My Background with Languages

I have used more than a dozen programming languages over the years. I started with Unix shell scripting and quickly moved on to Perl, ANSI C, PHP and TCL. I took courses in VB and Java, but never used them in production settings. Along the way I learned supporting tools like HTML, CSS, JavaScript and SQL. I've done small projects in Ada, VB, and Lua. There a few others that I played with to do small projects just for fun. I use PHP and C# on a daily basis now, but I don't' have exact syntax memorized. Modern development tools and search engines make it so that I don't have to remember syntax. I know the PATTERN, not the syntax. I know what languages are capable of not necessarily the exact commands to make it happen.

Don't Worry about Learning Syntax

The message I give my friends and family is that it doesn't matter what language you learn as long as it is interesting and you have a challenging but achievable goal in mind. Use your first language to learn the PATTERNS of programming that you can then apply to any language you take up. Pick a language, it doesn't matter which one, and dive into free tutorials on the web. Focus on the structure of what you're learning rather than the exact syntax. As you go, focus on the fundamentals that ALL languages share.

Then start working towards your goal. Your goal should be a simple site you've devised to learn the language. Having a goal is important because to learn a language you need to apply it. I am about to undertake writing an online game to teach myself some new technologies I've been interested in. Take a second to read the story of the dating site Plenty of Fish. It started as a project for the creator to teach himself ASP.net. The site went on to make millions.

A Core Set of Patterns to Know Exist

Output to the screen, console, or log file.

In order to follow along as a program is running you need it to print status messages somewhere. Whether you are logging data to a console, or printing to a webpage/screen, just know that almost all languages have a way for you to add visual feedback.

// javascript
console.log('this message will make it to the console.');
# php
echo "this message will make it to the screen/webpage.";

Adding comments or "commenting out" code.

Many languages include a way to add comments to the code that are ignored by the program interpreter or compiler. Comments can be used to add notes to the code. This is also very useful when you want to make a program skip a chunk of code while you are testing it.

// javascript
// Javascript uses two slashes to start a comment line.
# PHP
# In PHP you use a hash/pound sign to comment out a line
/* C# */
/* The slash+star starts a comment and the star+slash ends it. */

The slash+star method works in many languages and can be used for multi-line comments.

/* Actually, this works in C#, SQL, PHP and Javascript among others

This is often uses for MULTI Line Comments
so you don't have to add a comment designator the beginning of every line*/

You can also use the multi-line version to comment out entire chunks of code so the compiler or interpreter skips them.

/* this code will never get executed because it's commented out.
var myCondition = true;
if (myCondition) {
doSomething();
}
*/

Assigning Variables

All languages have a way to store data into the computer's memory so it can be retrieved or acted upon later. This is usually accomplished through creating a named variable. These variables can then be acted on in the future or control future logic branches.

// javascript
var myVariable = "This assigns a string of characters to a place in memory.";
console.log("The current state of the variable is: " + myVariable);
// php
$myVariable = "This assigns a string of character to a place in memory.";
echo "The current state of the variable is: $myVariable";

Conditions

A condition is, and I'm talking about the literal definition here, the state of something. In a programming language, these conditions are used in tests, or "conditional statements" to determine if something is TRUE or FALSE. True and False are the basis of ALL logic in programming. Remember, computers ultimately just know 1's and 0's.

// javascript
var myCondition = true; // set a variable to true;
// php
$myCondition = false; // set a variable to true.

Logic: If, Else, and Else If

All languages have a way to make decisions. You can use this to execute a chunk of code only when a condition is met. This is accomplished through the concept of an IF statement. IF something is true THEN do X. IF's are almost always paired with ELSE statements that then do the opposite. IF something is true THEN do X, Otherwise (else) do Y. And there is usually a concept of an ELSEIF that can be chained in there as well. IF something is true, do X, ELSEIF something else is true, do Y, Otherwise (else) do Z.

I like to joke that if you are smart enough to not touch a hot stove you are smart enough to code. You understand conditions and logic. If the stove is hot THEN don't touch it.

// javascript
var myCondition = true; // set a variable to true;
if (myCondition) {
console.log("The condition is true!");
} else {
console.log("The condition is false!");
}
// php
$myCondition = false; // set a variable to false.
if ($myCondition) {
echo "The condition is true!";
} else {
echo "The condition is false!";
}
// javascript, without the ELSE just so you know it's not required...
var myCondition = true; // set a variable to true;
if (myCondition) {
console.log("The condition is true!");
}

I did not remember off the top of my head the different formats for ELSEIF in different languages. I've done enough to know it's not always the same and was pretty sure I knew them, but since I'm throwing this on the Internet I went and searched "perl elseif" and "javascript elseif" to make sure I was right. That is the point, I know the pattern, not the syntax.

// javascript
var myCondition = true; // set a variable to true;
var someOtherCondition = true;
if (myCondition) {
console.log("The condition is true!");
} else if (someOtherCondition) {
console.log("myCondition is false, but someOtherCondition is true")
} else {
console.log("Both myCondition and someOtherCondition are false.");
}
// php
$myCondition = false; // set a variable to false.
$someOtherCondition = true;
if ($myCondition) {
echo "myCondition is true!";
} elseif ($someOtherCondition) {
echo "myCondition is false, but someOtherCondition is true."
} else {
echo "Both myCondition and someOtherCondition are false.";
}
# set a variable to true.
$myCondition = false;
$someOtherCondition = true;
if ($myCondition) {
echo "myCondition is true!";
} elsif ($someOtherCondition) {
echo "myCondition is false, but someOtherCondition is true."
} else {
echo "Both myCondition and someOtherCondition are false.";
}

So, in JavaScript it is "ELSE IF", in PHP it is "ELSEIF" and in Perl it is "ELSIF". Note, that in PHP, "ELSE IF" will work if you're using the curly braces, and in Perl ELSEIF will work just to yell at you about spelling it wrong, but the point is, know it's possible, and go search for the syntax when you need it.

Operators

An operator is something that takes one or more values and returns another value. This is getting into jargon here, but there are unary operators (take a single value) and binary operators (take two values) and even a ternary operator which takes three values. And you can string these together to form complex conditional statements. You will want to google common operators for the language you are working in, but here are some common examples:

The unary "logical NOT operator" ! (exclamation point) tests if something is NOT true.

// PHP
$myCondition = false; // set myCondition to false;

// instead of testing if something is true and then doing an ELSE...
if ($myCondition) {
// just a placeholder
} else {
echo "myCondition is NOT true";
}

// you can use the logical NOT operator ! to test if NOT myCondition
if (!$myCondition) {
echo "myCondition is NOT true;
}

The binary "logical AND operator" && tests if two things are true.

// PHP
$myCondition = true;
$myOtherCondition = true;

// you can use the logical NOT operator ! to test if NOT myCondition
if ($myCondition && $myOtherCondition) {
echo "both conditions are true;
} else {
echo "
One of the conditions is NOT true";
}

The binary "logical OR operator" || tests if either one of two things are true.

// PHP
$myCondition = true;
$myOtherCondition = false;

// you can use the logical NOT operator ! to test if NOT myCondition
if ($myCondition || $myOtherCondition) {
echo "One of the two conditions are true;
} else {
echo "
Both conditions are false";
}

A complex compilation of the three

// PHP
$myCondition = true;
$myOtherCondition = false;
$myThirdCondition = true;

if (($myCondition && $myOtherCondition) || !$myThirdCondition) {
// notice the grouping using parenthesis of multiple conditions.
echo "If myCondition AND myOtherCondition are both true, OR myThirdCondition is not True, we make it here.";
}

A comparison operator

// PHP
$myCondition = true;
$myOtherCondition = false;

// you use comparison operators to compare two things..
if ($myCondition == $myOtherCondition) {
echo "myCondition and myOtherCondition are the same."
}

There are also Assignment operators that help you when doing math such as incrementing, decrementing. There are also string assignment operators to make it easy to append strings together. There are less than 30 operators in the PHP language and I use 15 of them on a regular basis and a few of them rarely if ever. You can search for a complete list and get to know them for whatever language you are learning.

Loops: For and While

Loops are where the real magic of programming happens. The idea is that you repeat a certain task forever, until a condition is met, or iterate over a group of values. The most basic example is a loop that will repeat a task 100 times. You wouldn't want to copy/paste the same code line after line.

// PHP
/*
this uses the = assignment operator,
the <= comparison operator,
and the ++ increment math operator.

It is setting a variable, i, to 1, testing to see if it is less than 100
and then it will execute the code in the {}'s and then increment $i and repeat
until the condition $i < 100 is no longer met.
*/


for ($i = 1; $i <= 100; $i++) {
echo "I have run $i times.";
}

You can accomplish the same thing with a while loop...

// PHP
$i = 1;
while ($i <= 100) {
echo "I have run $i times.";
$i++;
}

Arrays, Collections, Lists

Most languages have a way of grouping multiple items together. Some languages call these arrays, some call them collections, and others have lists. Some languages have all three but use each slightly differently. To go along with these groups there is usually a foreach statement that lets you loop over the group.

// PHP
$groupOfPeople = array("Carson", "Dagny", "Cayla", "Ana");

foreach ($groupOfPeople as $name) {
echo "Say hi to $name";
}

We are starting to get into more complex topics now, but just to come back to the whole point, I did go to the PHP Manual for Foreach to make sure I got the exact syntax right. It differs from language to language, but know it's there so you recognize the pattern and can repeat it in whatever language you are working in.

Functions and Procedures

The last topic for this post is going to be functions. They are sometimes called a method, procedure, subroutine or subprogram. They are simply a named chunk of code that may or may not take values in as arguments and may or may not return a value. Nit-picking computer scientist know the difference between a procedure and a function, but you don't need to know the difference to use them. The key here is that you can use functions to isolate code that needs to be run many times so that you only have to write it once and then you can "call" or "invoke" the function from different parts of code or within a loop to write more compact, efficient, and easy to read code. A general rule of thumb is to have your functions do one thing well.

// PHP
/* this is just an example, as there are built in functions in PHP that
do these things and you'd want to use them instead of creating your own
functions to do them. */


$groupOfPeople = array("Carson", "Dagny", "Cayla", "Ana");

function findPersonInCrowd($person, $crowd) {
$location = 1;
foreach ($crowd as $name) {
if ($person == $name) {
return $location;
}
$location++;
}
return -1;
}

$carsonsLocationInCrowd = findPersonInCrowd("Carson", $groupOfPeople);
if ($carsonsLocationInCrowd == -1) {
echo "Carson is not in the crowd.";
} else {
echo "Carson is the " . $carsonLocationInCrowd . " person in the crowd.";
}

Here if you wanted to repeatedly find different peoples location in a group you wouldn't want to repeat that function's code over and over again. You'd want to write the function once and use it over and over. It's just an example, but almost every language has the concept of functions and knowing you can put code into functions so it can be repeatedly called is the pattern you want to know.

Input

I haven't covered inputs. Programs are usually acting on some kind of input from an end user, or a database, or some other system. Each language has its own way of getting to those inputs. From a pattern perspective just know that collecting inputs has already been solved and you can use a search engine to find specifics on how to collect data from users or other systems.

Design Patterns and Frameworks

When you get deeper into coding, you will run into the same problems over and over again. They usually have the same solution. There are entire courses in computer science pgorams that discuss known algorithms and Software Design Patterns. Many common problems are already solved and there is no good reason to re-solve them. Even in your own code, copy-and-paste is your friend. I often start new projects by loading up a framework I know has already solved a lot of what I'm working towards. When you get further along you'll know if you want to start a PHP website using Laravel or Drupal, or just roll your own. Don't think you're going to be starting every project form scratch.

Conclusuion

It doesn't matter what language you set out to learn, you are going to need operators, variables, logic structures, loops and functions. You'll also have inputs and some type of output. You'll want to add comments to your code. This is not just the tip of the iceberg, this is what programming is. If you've followed along to this point, you know enough to write a program. Now you just need to go do it. Don't be afraid to fail because you will. Over and over again. Search engines and tenacity will get you through.

If there are patterns I've missed please leave a comment below or send me an email.

I have a newsletter...

Many of my posts end up in Digital Ambit's monthly newsletter. It is the best way to keep up with what Dagny and I are doing in the business world. I appreciate your support and will only send you things we think are valuable.