DEV Community

loading...
Cover image for JavaScript Simple Code Structure

JavaScript Simple Code Structure

bello profile image Bello Updated on ・2 min read
This article is sponsored by Hostgator - Get Started today for $0.99

Statements

Statements generally are instructions to be executed by a computer program.

A JavaScript program or code is a list of programming statements

Statements in computer programming are executed from top to bottom (and left to right) in the order they appear.

console.log('Hello World!')
Enter fullscreen mode Exit fullscreen mode

When there is more than one statement in a JavaScript program, it is recommended to end statements with a semi-colon.

console.log('Good Day!');
console.log('Happy Codding!');
Enter fullscreen mode Exit fullscreen mode

Although without ending each statement with a semi-colon, computer programs will most likely execute successfully.

console.log('Good Day!')
console.log('Happy Codding!')
Enter fullscreen mode Exit fullscreen mode

There are a few cases when ending statements without semi-colon can lead to errors.

Edit on Stackblitz

It is also a good practice to end statements with semi-colon because it makes the code more readable and understandable.

console.log('Good Day!');
const func = num => {
    console.log(num);
};
[1, 2].forEach(func);
Enter fullscreen mode Exit fullscreen mode

In the example above, ending statements with a semi-colon makes your code readable because it marks the end of a statement.


image.png


White Space

Apart from ending each statement with a semi-colon for better readability, we can do more justice to make a program more readable.

console.log('Good Day!');
const func = num => {
    console.log(num);
};
[1, 2].forEach(func);
Enter fullscreen mode Exit fullscreen mode

It would be better to separate each separate set of independent instructions with a new line or white space.

console.log('Good Day!');  // independent of the code below

const func = num => {
    console.log(num);
};
[1, 2].forEach(func);
Enter fullscreen mode Exit fullscreen mode

Since words are usually separated by white space when writing a letter, an article, etc. In programming also, there's no exception to this rule.

let firstName = 'Osagie';
let lastName = 'Bello';
console.log('My name is ' + firstName + ' ' + lastName);
Enter fullscreen mode Exit fullscreen mode

Statements are composed of values (Osagie and Bello), Operators (+ and =), keywords (let), and expressions (statements with = in-between keyword and value)

A statement can also be an expression without an equal sign assignment operator = only if it can be evaluated.

5 * 4; // an expression
Enter fullscreen mode Exit fullscreen mode

Variables

Initialization of a variable to a value is done by an equal sign (=) in-between them.

let myName = 'Bello';
Enter fullscreen mode Exit fullscreen mode

The value (data) Bello is assigned to a variable name, myName in the example above. In computer programming, the equal sign which separates the value and variable is called an assignment operator.


camelCase

camelCase is a naming convention of variable, function, and class names when it contains more than one word.

let myName = 'Bello';
Enter fullscreen mode Exit fullscreen mode

myName are two words in a variable, my and name.

Happy Coding!!!


Buy me a Coffee


Techstack | Hostgator

Techstack article is sponsored by Hostgator.

  • A ton of website hosting options
  • 99.9% uptime guarantee
  • Free SSL certificate
  • Easy WordPress installs
  • A free domain for a year

Learn more

Discussion (0)

pic
Editor guide