JavaScript Basics - JavaScript: The Ultimate Crash Course Learning JavaScript within a Day with New Approach for Faster Programming (Save Time and Effort) (2016)

JavaScript: The Ultimate Crash Course Learning JavaScript within a Day with New Approach for Faster Programming (Save Time and Effort) (2016)

Chapter 1: JavaScript Basics

JavaScript is a programming language that is rich and expressive. It was established in 1991 by Sun Microsystems, from a team that was made of three individuals who were Mike Sheridan, Patrick Naughton and James Gosling. It used to be called Oak, and then it was called Green and in 1995, the name Java was given, having being named after the coffee. The basic concepts of JavaScript are the most important things a beginner needs to master. You should also know some of the pitfalls that have befallen people who have not used this expressive programing language before so that you can master it well and start programming like an expert.

JavaScript is a great place to start learning about programming, and with this knowledge, you will find it easier to master different programming languages. To begin with, we will explore the meaning of Syntax. Here are some syntax basics that should get you started:

Syntax Basics

JavaScript syntax refers to a set of rules that define a JavaScript program that has been well structured. Some of the important syntax that you need to learn as you get started in JavaScript programming include statements, variable naming, and white space among others. First of all, you need to know what a computer program. At its most basic, a computer program is the list of instructions that tell a computer what it must execute. In a programming language these program instructions are what we will call statements.

Case sensitivity: JavaScript is a case sensitive programming language. You will have to start the name of a constructor with a capital letter then the name of a function with a lower-case letter. This is important to remember as some other programming languages are not case sensitive, meaning that you have a different experience using them.

Whitespace: These are the spaces, the tabs and the newlines that are used outside of string constants. String constants can be identified as some letters and symbols that are used to create code in JavaScript programming language. Whitespace in JavaScript is very significant and it can directly impact semantics, which is not the case with some other programming languages like C.

Semicolon: Statements in JavaScript are usually separated by semicolons. There is a technique in JavaScript called Automatic Semicolon Insertion. This means that some statements that have been well formed after a new line has been parsed will be taken as complete, just the same way they would be considered if the semicolon were inserted just before the newline. You can opt for statements that terminate semicolons explicitly in order to reduce the unintended effects of the automatic semicolon insertion.

Here is an example of the semicolon use:

var a = 8;

var b = 9;

var c = a + b;

JavaScript Statements

In JavaScript, statements have several components which include: Values, Operators, Expressions, Keywords, and Comments.

Values: in JavaScript syntax, you will get to know about two types of values: the fixed values and the variable values. The fixed values are called the literals and the variable values are what we call variables.

The most important rules for writing JavaScript fixed values/literals are:

- Numbers can be written with or without decimals. Example:

10.50

1001

- Strings are a text and they are written within double or a single quote. Example:

"Alpha Delta"

'Alpha Delta'

JavaScript variables on the other hand, are used to store data values, just like in other programming languages. The var keyword will therefore be used to declare variables in the programming. An equal sign will be used to assign a value to a variable. Here is an example:

var a;

a = 8;

Here is an example of a simple variable declaration:

var foo = 'hello world';

The whitespace does not have significant meaning outside the quotation marks as illustrated:

var foo = 'hello world';

The parenthesis will indicate precedence as shown:

3 * 2 + 6; // returns 12; as first you have multiplication

3 * (5 + 3); // returns 24; as first you have addition

Tabs will have no meaning at all but they will enhance readability:

var foo = function(){

console.log('hello');

};

JavaScript expressions

An expression in JavaScript will be used to refer to the combination of values, variables and operators, all of which will compute to a certain value. The end computation will be called an evaluation. Here is an example of a simple expression:

2 * 10 evaluates to 20:

2 * 10

Expressions do contain variable values as well, a * 10.

The values used in such expressions can be of various types like say numbers and strings. "Alpha" + " " + "Delta", evaluates to "Alpha Delta": can be illustrated as:

"Alpha" + " " + "Delta"

JavaScript keywords

These are used to identify the actions that are to be performed. The variable keyword will be used to tell the browser to create a new variable. Example:

var a = 8 + 9;

var b = a * 10;

Comments

First of all, you need to realize that not all JavaScript statements end up being executed. The code that appears after double slashes // or in between /* and */ will be treated as a comment. The comments are supposed to be ignored, not to be executed. Example:

var a = 8; // I will be executed

// var a = 9; I will NOT be executed

Identifiers

Identifiers are basically names. In JavaScript, they will be used to name variables, keywords, functions and labels. Legal names behave the same and the rules remain the same in all programming languages. In JavaScript, the very first character has to be a letter, an underscore (_), or a dollar sign ($). The characters that will follow can be anything between letters, digits, underscores, or dollar signs. Note that numbers are not allowed as first characters. This makes it easy for one to distinguish between identifiers and numbers with ease in JavaScript.