diff --git a/docs/internals/contributing/writing-code/javascript.txt b/docs/internals/contributing/writing-code/javascript.txt
index 8dbd6e34f5..b75f01059d 100644
--- a/docs/internals/contributing/writing-code/javascript.txt
+++ b/docs/internals/contributing/writing-code/javascript.txt
@@ -56,13 +56,13 @@ Django's JavaScript tests use `QUnit`_. Here is an example test module:
QUnit.module('magicTricks', {
beforeEach: function() {
- var $ = django.jQuery;
+ const $ = django.jQuery;
$('#qunit-fixture').append('');
}
});
QUnit.test('removeOnClick removes button on click', function(assert) {
- var $ = django.jQuery;
+ const $ = django.jQuery;
removeOnClick('.button');
assert.equal($('.button').length, 1);
$('.button').click();
@@ -70,7 +70,7 @@ Django's JavaScript tests use `QUnit`_. Here is an example test module:
});
QUnit.test('copyOnClick adds button on click', function(assert) {
- var $ = django.jQuery;
+ const $ = django.jQuery;
copyOnClick('.button');
assert.equal($('.button').length, 1);
$('.button').click();
diff --git a/docs/ref/csrf.txt b/docs/ref/csrf.txt
index f181cc1fe1..95e2e83d5c 100644
--- a/docs/ref/csrf.txt
+++ b/docs/ref/csrf.txt
@@ -85,11 +85,11 @@ You can acquire the token like this:
.. code-block:: javascript
function getCookie(name) {
- var cookieValue = null;
+ let cookieValue = null;
if (document.cookie && document.cookie !== '') {
- var cookies = document.cookie.split(';');
- for (var i = 0; i < cookies.length; i++) {
- var cookie = cookies[i].trim();
+ const cookies = document.cookie.split(';');
+ for (let i = 0; i < cookies.length; i++) {
+ const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
@@ -99,14 +99,14 @@ You can acquire the token like this:
}
return cookieValue;
}
- var csrftoken = getCookie('csrftoken');
+ const csrftoken = getCookie('csrftoken');
The above code could be simplified by using the `JavaScript Cookie library
`_ to replace ``getCookie``:
.. code-block:: javascript
- var csrftoken = Cookies.get('csrftoken');
+ const csrftoken = Cookies.get('csrftoken');
.. note::
@@ -137,7 +137,7 @@ and read the token from the DOM with JavaScript:
{% csrf_token %}
Setting the token on the AJAX request
@@ -148,7 +148,7 @@ Finally, you'll need to set the header on your AJAX request. Using the
.. code-block:: javascript
- var request = new Request(
+ const request = new Request(
/* URL */,
{headers: {'X-CSRFToken': csrftoken}}
);
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
index a8e8ad98f8..54258155bd 100644
--- a/docs/ref/templates/builtins.txt
+++ b/docs/ref/templates/builtins.txt
@@ -1830,7 +1830,7 @@ The resulting data can be accessed in JavaScript like this:
.. code-block:: javascript
- var value = JSON.parse(document.getElementById('hello-data').textContent);
+ const value = JSON.parse(document.getElementById('hello-data').textContent);
XSS attacks are mitigated by escaping the characters "<", ">" and "&". For
example if ``value`` is ``{'hello': 'world&'}``, the output is:
diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt
index 1fa2c7e711..acb522c603 100644
--- a/docs/topics/i18n/translation.txt
+++ b/docs/topics/i18n/translation.txt
@@ -1080,9 +1080,12 @@ interface within your Python code::
The ``ngettext`` function provides an interface to pluralize words and
phrases::
- var object_count = 1 // or 0, or 2, or 3, ...
- s = ngettext('literal for the singular case',
- 'literal for the plural case', object_count);
+ const objectCount = 1 // or 0, or 2, or 3, ...
+ const string = ngettext(
+ 'literal for the singular case',
+ 'literal for the plural case',
+ objectCount
+ );
``interpolate``
~~~~~~~~~~~~~~~
@@ -1096,23 +1099,29 @@ function supports both positional and named interpolation:
corresponding ``fmt`` placeholders in the same order they appear.
For example::
- fmts = ngettext('There is %s object. Remaining: %s',
- 'There are %s objects. Remaining: %s', 11);
- s = interpolate(fmts, [11, 20]);
- // s is 'There are 11 objects. Remaining: 20'
+ const formats = ngettext(
+ 'There is %s object. Remaining: %s',
+ 'There are %s objects. Remaining: %s',
+ 11
+ );
+ const string = interpolate(formats, [11, 20]);
+ // string is 'There are 11 objects. Remaining: 20'
* Named interpolation: This mode is selected by passing the optional
boolean ``named`` parameter as ``true``. ``obj`` contains a JavaScript
object or associative array. For example::
- d = {
- count: 10,
- total: 50
+ const data = {
+ count: 10,
+ total: 50
};
- fmts = ngettext('Total: %(total)s, there is %(count)s object',
- 'there are %(count)s of a total of %(total)s objects', d.count);
- s = interpolate(fmts, d, true);
+ const formats = ngettext(
+ 'Total: %(total)s, there is %(count)s object',
+ 'there are %(count)s of a total of %(total)s objects',
+ data.count
+ );
+ const string = interpolate(formats, data, true);
You shouldn't go over the top with string interpolation, though: this is still
JavaScript, so the code has to make repeated regular-expression substitutions.