0% found this document useful (0 votes)
116 views8 pages

JavaScript Comments Explained

JavaScript: Syntax

Uploaded by

rowrowpoc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views8 pages

JavaScript Comments Explained

JavaScript: Syntax

Uploaded by

rowrowpoc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

 Tutorials  Exercises  Services   Upgrade Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTST

JavaScript Syntax
❮ Previous Next ❯

Syntax Rules
Rules how Programs Must be Constructed

// How to Declare variables:


let x = 5;
let y = 6;

// How to Compute values:


let z = x + y;

// I am a Comment. I do Nothing

JavaScript Values
The JavaScript syntax defines two types of values:

Literals (Fixed values)


Variables (Variable values)

JavaScript Literals
The most important syntax rules for literals (fixed values) are:

Numbers are written with or without decimals:

10.50
1001
 Tutorials  Exercises  Services   Upgrade Get Certified Sign In

 Try itCSS
HTML YourselfJAVASCRIPT
» SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTST

Strings are text, written within double or single quotes:

"John Doe"

'John Doe'

Try it Yourself »

JavaScript Variables
Variables are containers for storing data values.

Variables must be identified with unique names.

Example

// Define x as a variable
let x;

// Assign the value 6 to x


x = 6;

Try it Yourself »

JavaScript Identifiers
Identifiers are used to name variables and keywords, and functions.

The rules for legal names are the same in most programming languages:

A name must begin with A name can contain

A letter (A-Z or a-z) A letter (A-Z or a-z)

A number (0-9)
A dollarTutorials
sign ($) Exercises  Services   A dollar Upgrade
sign ($) Get Certified Sign In

An underscore (_) An underscore (_)


HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTST

Note
Numbers are not allowed as the first character in names.

This way JavaScript can easily distinguish identifiers from numbers.

JavaScript Keywords
JavaScript keywords are used to defines actions to be performed.

The let and const keywords create variables:

Example

let x = 5;
const fname = "John";

Try it Yourself »

JavaScript Operators
JavaScript assignment operators (=) assign values to variables:

Example
let x = 5;
let y = 6;
let sum = x + y;

Try it Yourself »

JavaScript uses arithmetic operators ( + - * / ) to compute values:


 Tutorials 
Example
Exercises  Services   Upgrade Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTST
5 * 10

Try it Yourself »

JavaScript Expressions
An expression is a combination of values, variables, and operators, which computes to a value.

Examples
(5 + 6) * 10 evaluates ti 110:

(5 + 6) * 10

Try it Yourself »

Expressions can also contain variable:

x * 10

Try it Yourself »

"John" + " " + "Doe", evaluates to "John Doe":

"John" + " " + "Doe"

Try it Yourself »

JavaScript Comments
Code after double slashes // or between /* and */ is treated as a comment.

Comments are ignored, and will not be executed:


 Tutorials 
Example Exercises  Services   Upgrade Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTST
let x = 5; // I will be executed

// x = 6; I will NOT be executed

Try it Yourself »

Note
You will learn more about comments in a later chapter.

JavaScript is Case Sensitive


All JavaScript identifiers are case sensitive.

The variables lastName and lastname , are two different variables:

Example
let lastName = "Doe";
let lastname = "Peterson";

Try it Yourself »

Note
JavaScript does not interpret LET or Let as the keyword let.

JavaScript and Camel Case


Historically, programmers have used different ways of joining multiple words into one variable name:

Hyphens:
first-name, last-name, master-card, inter-city.
 Tutorials  Exercises  Services   Upgrade Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTST

Note
Hyphens are not allowed in JavaScript. They are reserved for subtractions.

Underscore:

first_name, last_name, master_card, inter_city.

Upper Camel Case (Pascal Case):

FirstName, LastName, MasterCard, InterCity.

Lower Camel Case:

JavaScript programmers tend to use camel case that starts with a lowercase letter:

firstName, lastName, masterCard, interCity.

?
Exercise
What is a correct syntax for assigning a value to a variable?

x : 5

x = 5

x == 5

x -> 5

Submit Answer »

Video: JavaScript Syntax


 Tutorials  Exercises  Services   Upgrade Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTST

❮ Previous Next ❯

Track your progress - it's free! Sign Up Log in

COLOR PICKER
    
 Tutorials  Exercises  Services  Upgrade Get Certified Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTST

 PLUS SPACES GET CERTIFIED

FOR TEACHERS FOR BUSINESS CONTACT US

Top Tutorials Top References


HTML Tutorial HTML Reference
CSS Tutorial CSS Reference
JavaScript Tutorial JavaScript Reference
How To Tutorial SQL Reference
SQL Tutorial Python Reference
Python Tutorial [Link] Reference
[Link] Tutorial Bootstrap Reference
Bootstrap Tutorial PHP Reference
PHP Tutorial HTML Colors
Java Tutorial Java Reference
C++ Tutorial Angular Reference
jQuery Tutorial jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].

Common questions

Powered by AI

JavaScript expressions combine values, variables, and operators to compute a final value. When expressions contain only literals, the computed result is purely based on these constant values without any dependency on external factors. For instance, (5 + 6) * 10 is a literal-only expression resulting in 110. In contrast, expressions including variables, such as x * 10, will have outcomes influenced by the current values of those variables, allowing for more dynamic calculations that can change depending on variable assignments .

In dynamic web applications, the use of predefined literal values (e.g., fixed numbers or strings) can introduce challenges such as decreased flexibility and maintainability. Literal values hardcoded into JavaScript can make future updates cumbersome; altering functionality often requires modifying the source code rather than simply updating configuration or variable assignments. This rigidity can complicate localization, scaling, and customization of applications, potentially leading to errors if literals are not systematically managed and abstracted .

JavaScript's case sensitivity implies that variables and function identifiers with the same name but differing cases are treated as distinct entities, which impacts variable management and function execution significantly. For example, 'variableName' and 'variablename' are considered separate by the JavaScript engine. This requires developers to maintain consistent naming conventions to avoid referencing unintended variables or functions, reducing potential bugs, and improving code clarity. Understanding this trait is crucial in complex scripts with many identifiers .

Operators in JavaScript, such as arithmetic (+, -, *, /), assignment (=), and logical operators, are fundamental in evaluating expressions and assigning values. Their correct use enables the construction of complex logic and computation sequences. For instance, arithmetic operators facilitate mathematical calculations, while assignment operators link variables to data. Misuse or misunderstanding of operator precedence, however, can lead to unexpected results and logic errors. Therefore, understanding operator functionality and precedence is crucial for accurate and predictable expression evaluations .

JavaScript identifiers must follow specific rules to ensure code is syntactically correct and interpretable by the compiler. An identifier must start with a letter (A-Z or a-z), a dollar sign ($), or an underscore (_), and can further contain letters, numbers, dollar signs, or underscores, but not spaces or hyphens. Additionally, identifiers are case-sensitive and cannot begin with numbers to clearly distinguish them from numeric literals. Adhering to these rules prevents syntax errors, ensures variable uniqueness, and maintains consistent code readability .

In JavaScript, 'let' and 'const' are keywords used to declare variables. 'let' allows you to declare variables that can have their value reassigned within its scope, making it suitable for use within loops or conditionals where the value might change. In contrast, 'const' is used to declare variables that should not be reassigned after their initial assignment, ensuring the value remains constant. This distinction helps manage variable immutability and state within code blocks .

Comments in JavaScript are non-executable lines of code used to annotate or clarify the purpose of a particular code segment, helping to improve code readability and maintainability. Comments are indicated by either double slashes // for single-line or between /* and */ for multi-line comments. They do not affect the execution of a script directly as they are ignored during code execution, allowing developers to include explanations, notes, or temporarily disable code without removing it .

Different naming conventions like camel case and underscore serve to improve code readability and organization in JavaScript by visually distinguishing variables within their context and use case. Camel case (e.g., firstName) is preferred for JavaScript because it visually groups words without using characters that the language reserves for operators, such as hyphens. Underscore (e.g., first_name) can make variables appear distinct and is sometimes used for private variables. These conventions ensure clarity in multi-word identifiers and help maintain a consistent coding style, which is critical for collaborative development .

Proper variable scope management in complex JavaScript programs involves systematically using functions to encapsulate state and employing block scope declarations ('let' and 'const') to contain variables within minimal contexts. This reduces the risk of unintentional overwrites and variable leaking into global scope. Utilizing immediately invoked function expressions (IIFE) and modules or closures can further isolate scope, enhancing control over variable access and modification. Consistent use of these methodologies helps maintain application stability and clarity, especially in collaborative projects .

Consider the expression 'let x = 10; let y = 5; let result = (x + 5) * y;'. This combines both variables (x, y) and a literal (5). During evaluation, the expression inside the parentheses is calculated first: x (10) plus the literal 5 results in 15. Next, this sum is multiplied by y (5), yielding a final result of 75. The order of operations (parentheses before multiplication) governs this sequence, demonstrating how variables can dynamically alter expression outcomes depending on their assigned values .

You might also like