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

JavaScript Syntax

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)
13 views8 pages

JavaScript Syntax

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
You are on page 1/ 8

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

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS 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 W3.CSS 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 W3.CSS 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 W3.CSS 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 W3.CSS 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 W3.CSS 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 W3.CSS 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 W3.CSS 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 W3.CSS Reference
W3.CSS 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
W3.CSS 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 W3.CSS.

You might also like