4 Notes - Lectue I-3773
4 Notes - Lectue I-3773
Code Structure
Statement
Statements are syntax constructs and commands that perform actions.
We’ve already seen a statement, alert('Hello, world!'), which shows the message
“Hello, world!”.
Semicolon
A semicolon may be omitted in most cases when a line break exists.
There are cases when a newline does not mean a semicolon. For example:
Comments
As time goes on, programs become more and more complex. It becomes necessary
to add comments which describe what the code does and why.
Comments can be put into any place of a script. They don’t affect its execution
because the engine simply ignores them.
The rest of the line is a comment. It may occupy a full line of its own or follow a
statement.
Multiline comments start with a forward slash and an asterisk /* and end with an
asterisk and a forward slash */.
Like this:
Variables
Most of the time, a JavaScript application needs to work with information. Here are two
examples.
● An online shop – the information might include goods being sold and a shopping
cart.
● A chat application – the information might include users, messages, and much
more. Variables are used to store this information.
A Variables
A variable is a “named storage” for data. We can use variables to store goodies, visitors,
and other data.
The statement below creates (in other words: declares) a variable with the name
“message”:
● Variable names must start with a letter, an underscore (_) or a dollar sign ($).
● Variable names cannot contain spaces.
● Variables cannot be the same as reserved keywords such as if or const.
● By convention, JavaScript variable names are written in camelCase.
● Variables should be given descriptive names that indicate their content and
usage (e.g. sellingPrice and costPrice rather than x and y).
● As JavaScript variables do not have set types, it can be useful to include an
indication of the type in the name (e.g. orderNumber is obviously a numeric
ID, whereas order could be an object, a string or anything else).
LET KEYWORD
Syntax:
Variables defined by let can not be redeclared but can be reassigned but can be
redeclared
Let x= 8;
x=16;
Console.log (x);
The output for the above code will be 16 since you are redeclaring the variable x.
VAR KEYWORD
Var keyword is used to declare variables.
Syntax:
CONST KEYWORD
Const keyword is used to declare variables.Variables which are defined using const
can not be changed after assigning.
Syntax:
Data types
A data type is a classification of the type of data that can be stored and manipulated within a
program.
Each data type has its own characteristics and methods for manipulation.
1. Primitive data types: These basic data types represent a single value. JavaScript has
seven primitive data types. They include:
Example:
The primitive wrapper types make it easier to use primitive values including
booleans, numbers, and strings.
In this example, The variable language holds a primitive string value. It doesn’t have
any method like charAt(). However, the above code works perfectly.
Methods in Strings
Strings are useful for holding data that can be represented in text form. Some of the
most-used operations on strings are to check their length, to build and concatenate
them using the + and += string operators, checking for the existence or location of
substrings with the indexOf() method, or extracting substrings with the substring()
method.